having a problem with an instance of a method.

john.mcginnis at comcast.net john.mcginnis at comcast.net
Fri Jul 14 12:06:17 EDT 2006


I wanted to dig a little deeper understanding of Classes, Methods and
Instances. However I get this - ' AClassA instance has no attribute' as
an error when I call the method I am using as a learning tool.

Here's the code, from hammer.py
---------------------------------------------------------------------------------
class AClassA:

    def __init__(self):
        self.a = 0

    def printit(self):
        print "done from Class a"

class BClassB:

    def __init__(self):
         self.a = 0

    def printit(self):
         print "done from Class b"

Here is the call
---------------------------------------------------------------------------------
Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from hammer import *
>>> x = AClassA()
>>> x.printit()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: AClassA instance has no attribute 'printit'
>>> dir(AClassA)
['__doc__', '__init__', '__module__']
>>>

---------------------------------------------------------------------------------
I can see that AClassA has no method defined so the error makes sense.

But I can't figure out why 'def printit(self): is not included as a
component of the class AClassA. The syntax looks right.

Observations on what I am doing wrong?




More information about the Python-list mailing list