Trying to print from inside a method

Alex Martelli aleax at mac.com
Sun Mar 18 19:32:17 EDT 2007


<AWasilenko at gmail.com> wrote:

> 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#39>", 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.message2 = ...whatever else...

Weirder and weirder, but still legal Python.

> >>> print test1.message2
> 
> Traceback (most recent call last):
>   File "<pyshell#40>", 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#41>", 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



More information about the Python-list mailing list