Attribute error-- but I'm innocent(?)

N

Nick Mellor

Hi all,

I'm pretty sure I'm following all the Python rules: I've put "self"
before "forename" to make sure it's treated as a data attribute
(instance variable.) And from within a class, I'm told, you need to
prefix the var with self too. RandomName is a class that I've tested
(and which still works.) So why do I get this error?

File "h:\Testing\NameDb\dictfile.py", line 107, in randomName
return {"Forename" : self.forename.randomByWeight(),
AttributeError: RandomPerson instance has no attribute 'forename'

Here's the code (Python 2.6, PythonWin):

class RandomPerson:
def __init(self):
self.forename = RandomName("h:\\Testing\\NameDb\
\Forenames.csv", namefield = "Forename")
self.surname = RandomName("h:\\Testing\\NameDb\\Surnames.csv",
namefield = "Surname")
self.randomAddress = dictfile("h:\\Testing\\NameDb\
\Addresses.csv").cycleShuffled()
[...]

def randomName(self):
return {"Forename" : self.forename.randomByWeight(),
"Surname" : self.surname.randomByWeight()}

if __name__ == "__main__":
person = RandomPerson()
print person.randomName()
 
C

Chris Rebert

Hi all,

I'm pretty sure I'm following all the Python rules: I've put "self"
before "forename" to make sure it's treated as a data attribute
(instance variable.) And from within a class, I'm told, you need to
prefix the var with self too. RandomName is a class that I've tested
(and which still works.) So why do I get this error?

 File "h:\Testing\NameDb\dictfile.py", line 107, in randomName
   return {"Forename" : self.forename.randomByWeight(),
AttributeError: RandomPerson instance has no attribute 'forename'

Here's the code (Python 2.6, PythonWin):

class RandomPerson:
   def __init(self):

That previous line is supposed to be:
def __init__(self):

Note the trailing underscores.

Cheers,
Chris
 
J

John Machin

Hi all,

I'm pretty sure I'm following all the Python rules: I've put "self"
before "forename" to make sure it's treated as a data attribute
(instance variable.) And from within a class, I'm told, you need to
prefix the var with self too. RandomName is a class that I've tested
(and which still works.)

Doesn't look like you've executed RandomName() in this particular
example.

So why do I get this error?

  File "h:\Testing\NameDb\dictfile.py", line 107, in randomName
    return {"Forename" : self.forename.randomByWeight(),
AttributeError: RandomPerson instance has no attribute 'forename'

Next time, show the *full* traceback.
Here's the code (Python 2.6, PythonWin):

class RandomPerson:
    def __init(self):

Because you named this method __init instead of __init__

Next time, before proclaiming innocence, insert a print statement or
two:
print "Hello from RandomPerson.__init"
and wonder what the problem is if the print statement is not executed
        self.forename = RandomName("h:\\Testing\\NameDb\
\Forenames.csv", namefield = "Forename")
        self.surname = RandomName("h:\\Testing\\NameDb\\Surnames.csv",
namefield = "Surname")
        self.randomAddress = dictfile("h:\\Testing\\NameDb\
\Addresses.csv").cycleShuffled()
[...]

    def randomName(self):
        return {"Forename" : self.forename.randomByWeight(),
                "Surname" : self.surname.randomByWeight()}

if __name__ == "__main__":
    person = RandomPerson()

and here's another good place for a print statement:
print person.__dict__
    print person.randomName()

Cheers,
John
 
N

Nick Mellor

Thanks Chris and John, all workin now. Sorry about proclamation of
innocence-- fruitless morning and 3 hours sleep :)

Nick
 
D

Denis Kasak

On Mon, 2 Mar 2009 16:56:58 -0800 (PST), Nick Mellor


       Where is "RandomName" defined? Python is case sensitive, and the
method further down is "randomName".

You could ask the same for dictfile(), which is also not shown, though
you could probably deduce from the filename (and the fact that it
works now) that it is defined elsewhere in the same file.
       Furthermore, given the indentation you show, randomName is a method
of RandomPerson... If that is true, you'll need to do

               target = self.randomName(...)
        self.surname = RandomName("h:\\Testing\\NameDb\\Surnames.csv",
namefield = "Surname")
        self.randomAddress = dictfile("h:\\Testing\\NameDb\
\Addresses.csv").cycleShuffled() it should be obvious
[...]

    def randomName(self):

As the OP said, RandomName is a class and, judging by the
capitalization, he is instantiating objects of the said class and not
the method of RandomPerson.
       Up above you are pass two arguments (besides the "self" with my
noted change would supply), but here you do not have anything to accept
them... Where is that file name and that keyword argument supposed to be
received?

       def randomName(self, fileid, namefield):
       #have to use "namefield" as you used a keyword passing mechanism

       This will return a dictionary containing both forename and
surname... Assuming you have forename and surname OBJECTS with contain a
randomByWeight method.

In all probability, forename and surname *are* instances of RandomName
and contain the randomByWeight() method.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top