Trying to print from inside a method

AWasilenko at gmail.com AWasilenko at gmail.com
Sun Mar 18 18:15:16 EDT 2007


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.message22()
This is message 2
>>> test2.message22()
This is message 2
>>> print test1.message22.message2

Traceback (most recent call last):
  File "<pyshell#39>", 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#40>", 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#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




More information about the Python-list mailing list