best way to get data into a new instance?

Maxim Sloyko m.sloyko at gmail.com
Fri Sep 29 09:04:23 CEST 2006


tobiah wrote:
[snip]
> 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
this:

def __init__(self, id, **kw):
    self.id = id
    for name in ['first_name', 'last_name', 'age', 'salary',
'whatever']:
        self.__dict__[name] = kw.get(name, None)

--
Maxim Sloyko




More information about the Python-list mailing list