having a problem with an instance of a method.

J

john.mcginnis

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.Traceback (most recent call last):
File said:
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?
 
D

Dennis Lee Bieber

Here's the code, from hammer.py

Is that a cut&paste from an editor, a retype by hand, or an
insert-text-file (for those clients that support such).
---------------------------------------------------------------------------------
class AClassA:

def __init__(self):
self.a = 0

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

If your editor has tabs set to 4-spaces, and you entered (Note --
the alignment will vary depending on editor settings):

class AClass:
def __init__(self): #4 spaces
pass #8 spaces

def printIt(self): #1 tab
print "This is AClass" #2 tabs

.... then even though the "def" look to line up, the second one is
actually being defined inside the first one, not at the same level as
it.

This could happen if you started editing a file that used spaces,
and you used tabs.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top