Trying to print from inside a method

A

AWasilenko

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
Traceback (most recent call last):
File "<pyshell#39>", line 1, in -toplevel-
print test1.message22.message2
AttributeError: 'function' object has no attribute 'message2'
Traceback (most recent call last):
File "<pyshell#40>", line 1, in -toplevel-
print test1.message2
AttributeError: 'First' object has no attribute 'message2'
Traceback (most recent call last):
File "<pyshell#41>", 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
 
A

Alex Martelli

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.
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).
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
 
A

AWasilenko

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
 
A

Alex Martelli

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top