best way to get data into a new instance?
m.sloyko at gmail.com
Fri Sep 29 09:04:23 CEST 2006
> class Employee:
> __init__(self, id):
> self.id = id
> e = Employee(32445)
> ... later
> e.firstname = 'foo'
> e.lastname = 'bar'
> ... more as the information comes about.
Personally, I think that this is not a good solution. How would the
reader of your code guess what properties your object has? If you don't
like to write too many assignments, you can at least do something like
def __init__(self, id, **kw):
self.id = id
for name in ['first_name', 'last_name', 'age', 'salary',
self.__dict__[name] = kw.get(name, None)
More information about the Python-list