FuriousLettuce
May 8th, 2007, 06:23 PM
I've only just started learning python in the last day or so, and I'm trying to create a simple function (inside a class getID()) that grabs some of the details of the user. Currently, I've got
class getID:
"Gets the details of users by standard input"
def getDetails(self):
self.items = {"DOB" : "", "nationality" : "", "race" : ""}
for e in self.items:
self.items[e] = raw_input("What is your %s? " % e)
This works in that I can do:
x = getID()
x.getDetails()
print x.items["DOB"], x.items["nationality"], x.items["race"]
However, I'm trying to modify it so I don't need the items dictionary, so I can just reference x.DOB, x.nationality etc. I tried
for e in items
self.e = raw_input(...)
This only appears to create a variable self.e, rather than self.DOB etc. I imagine that I might have to do something with __init__? I realise I could use UserDict to define x["DOB"] etc, but it would be nicer to reference as x.DOB. Does anybody know of a way to do this, and does anybody know what the preferred method would usually be?
PS Might somebody explain to me when convention dictates that my methods should begin def __xyz(self) rather than just def xyz(self)?
class getID:
"Gets the details of users by standard input"
def getDetails(self):
self.items = {"DOB" : "", "nationality" : "", "race" : ""}
for e in self.items:
self.items[e] = raw_input("What is your %s? " % e)
This works in that I can do:
x = getID()
x.getDetails()
print x.items["DOB"], x.items["nationality"], x.items["race"]
However, I'm trying to modify it so I don't need the items dictionary, so I can just reference x.DOB, x.nationality etc. I tried
for e in items
self.e = raw_input(...)
This only appears to create a variable self.e, rather than self.DOB etc. I imagine that I might have to do something with __init__? I realise I could use UserDict to define x["DOB"] etc, but it would be nicer to reference as x.DOB. Does anybody know of a way to do this, and does anybody know what the preferred method would usually be?
PS Might somebody explain to me when convention dictates that my methods should begin def __xyz(self) rather than just def xyz(self)?