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

Nick Mellor nick.mellor.groups at
Tue Mar 3 01:56:58 CET 2009

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\", 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\

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

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

More information about the Python-list mailing list