Trying to print from inside a method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AWasilenko@gmail.com

    #1

    Trying to print from inside a method

    I'm still in the process of learning python via a handful of books I
    bought. One book I am reading just introduced Base Class Methods. I
    found that I needed more understanding on this concept and wrote a
    short test program using the Author's code as a vague reference. My
    question now really isn't Base Class related, but my question stems
    from my test code so I will just post it as is.

    ##Test of Super() stuff
    class First(object):

    def __init__(self, wamba, nextel):
    self.wamba = wamba
    self.nextel = nextel
    message1 = "This is message 1"
    print message1

    def message22(self) :
    message2 = "This is message 2"
    print message2

    class Second(First):

    def __init__(self, samba, fextel, sony):
    super(Second, self).__init__( samba, fextel)
    self.sony = sony
    print "This is message 1a"


    test1 = First(wamba = "Mermaid Man", nextel = "Cell Phone")
    test2 = Second(samba = "Barnical Boy", fextel = "Hooopla", sony =
    "Nooo Good!")

    My question came up when I wanted to print message1 directly and
    couldn't figure out how to do it. I then added the message22 def
    thinking that perhaps you couldn't print from constructor methods for
    some reason. I then added the print line in the message22 def, just
    out of desperation, and was able to make that work, but is not what I
    wanted. Here is my copy from my IDEL attempts:

    This is message 1
    This is message 1
    This is message 1a
    >>test1.message 22()
    This is message 2
    >>test2.message 22()
    This is message 2
    >>print test1.message22 .message2
    Traceback (most recent call last):
    File "<pyshell#3 9>", line 1, in -toplevel-
    print test1.message22 .message2
    AttributeError: 'function' object has no attribute 'message2'
    >>print test1.message2
    Traceback (most recent call last):
    File "<pyshell#4 0>", line 1, in -toplevel-
    print test1.message2
    AttributeError: 'First' object has no attribute 'message2'
    >>print First.message22 .message2
    Traceback (most recent call last):
    File "<pyshell#4 1>", line 1, in -toplevel-
    print First.message22 .message2
    AttributeError: 'function' object has no attribute 'message2'
    >>>
    I know this is a very basic question but it's the stupid ones that
    always get me.

    - Adam

  • Alex Martelli

    #2
    Re: Trying to print from inside a method

    <AWasilenko@gma il.comwrote:
    I'm still in the process of learning python via a handful of books I
    bought. One book I am reading just introduced Base Class Methods. I
    I believe your problem may be more basic: local variables, istance
    attributes, and class variables, are very separate things.
    found that I needed more understanding on this concept and wrote a
    short test program using the Author's code as a vague reference. My
    question now really isn't Base Class related, but my question stems
    from my test code so I will just post it as is.
    >
    ##Test of Super() stuff
    class First(object):
    >
    def __init__(self, wamba, nextel):
    self.wamba = wamba
    self.nextel = nextel
    message1 = "This is message 1"
    print message1
    >
    def message22(self) :
    message2 = "This is message 2"
    print message2
    Here, the message1 and message2 names are LOCAL variables of the
    respective methods: each disappear as soon as its method ends.

    If you want to make them into INSTANCE attributes, so they'll stick
    around for later, assign to self.message1 and self.message2 instead (and
    of course use these composite names too if need be).
    Traceback (most recent call last):
    File "<pyshell#3 9>", line 1, in -toplevel-
    print test1.message22 .message2
    AttributeError: 'function' object has no attribute 'message2'
    this would require making message2 an attribute of another attribute
    called message22 -- i.e., the function itself (methods don't have
    attributes, but their underlying functions can; the difference is that
    separate instances of the method refer to the same underlying function,
    so they would all share the same attribute).

    IOW, you'd have in the body of message22 to write:

    self.message22. mesage2 = whatever

    This would be pretty weird, but legal Python.

    This would only work if you CALLED test1.message22 () at some point, of
    course; the method's body is not executed until you call it. If you
    want this attribute to appear w/o the need to call the method, you need
    to assign it in the body of class First after the end of the def for
    message22:

    def message22(self) : ...whatever...

    message22.messa ge2 = ...whatever else...

    Weirder and weirder, but still legal Python.
    >print test1.message2
    >
    Traceback (most recent call last):
    File "<pyshell#4 0>", line 1, in -toplevel-
    print test1.message2
    AttributeError: 'First' object has no attribute 'message2'
    And this is the one you'd solve by having, in the method's body,

    self.message2 = ...blah blah...

    but only after the method has been called (move this assignment to
    __init__ if you want it to take action without need to call the method).
    >print First.message22 .message2
    >
    Traceback (most recent call last):
    File "<pyshell#4 1>", line 1, in -toplevel-
    print First.message22 .message2
    AttributeError: 'function' object has no attribute 'message2'
    This, again, you'd fix by assigning to the attribute of function
    message22, just like the very first case.


    To recap: attributes can be accessed on the objects you have assigned
    them too (with some latitude: you can access on an instance an attribute
    of its class or any baseclass thereof, you can access on a method an
    attribute of is underlying function). Local variables -- assignment you
    do within a function (or method) to bare names -- disappear as soon as
    the method (or function) is done executing.


    Alex

    Comment

    • AWasilenko@gmail.com

      #3
      Re: Trying to print from inside a method

      On Mar 18, 7:32 pm, a...@mac.com (Alex Martelli) wrote:
      Here, the message1 and message2 names are LOCAL variables of the
      respective methods: each disappear as soon as its method ends.
      >
      If you want to make them into INSTANCE attributes, so they'll stick
      around for later, assign to self.message1 and self.message2 instead (and
      of course use these composite names too if need be).
      Ah ha, the nugget of info I was forgetting! Looking back now I can
      see why the
      author used the self.whatever, and why my code is not working. When
      I'm reading along
      all his code looks find and dandy, makes perfect sense; but soon as I
      try to do
      something on my own quickly find that my understanding of how things
      work is very
      porous :)
      This would be pretty weird, but legal Python.
      Weirder and weirder, but still legal Python.
      This is usually how most of my code turns out :( I'm afraid if I ever
      learn enough
      to make a serious program I will be burned at the stake for such un-
      pythonic practices :P

      Comment

      • Alex Martelli

        #4
        Re: Trying to print from inside a method

        <AWasilenko@gma il.comwrote:
        ...
        This would be pretty weird, but legal Python.
        Weirder and weirder, but still legal Python.
        >
        This is usually how most of my code turns out :( I'm afraid if I ever
        learn enough
        to make a serious program I will be burned at the stake for such un-
        pythonic practices :P
        Function attributes are rarely used, because they belong to "the
        function object itself", not to any given _call_ to that function, or to
        any _method_ that may wrap that function, and so on. That's what pretty
        weird here -- it would not appear that you necessarily want to associate
        those attributes with the function object, and yet (if you want them to
        access as attributes of the function object) you do of course need to do
        that. Of course, it's somewhat hard to divine "semantic intent of the
        programmer" from a totally abstract toy example, so I could be wrong!


        Alex


        Comment

        Working...