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()
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()