Ruby Impressions (fwd) (fwd)

Terry Reedy tjreedy at home.com
Sat Jan 12 11:52:41 EST 2002


"Lulu of the Lotus-Eaters" <mertz at gnosis.cx> wrote in message
news:mailman.1010821638.23335.python-list at python.org...
> adamspitz at bigfoot.com (Adam Spitz) wrote:
> | class Person:
> |     def __init__(self, name, age, gender):
> |         self.name, self.age, self.gender = name, age, gender
> |         self.doSomeStuff()
> |I'd like to be able to tell Python, "Make me an __init__ method
that
> |takes these three parameters, stores them as attributes, and then
does
> |this other stuff."
>
> Like this?
>
>   >>> class Person:
>   ...     def __init__(self, **args):
>   ...         self.__dict__.update(args)
>   ...
>   >>> p = Person(name='Joe',age=37,gender='M/F')
>   >>> p.name
>   'Joe'

The price of this simplicity is loss of control as to what attributes
get stored.

Adam, you actually could write a function make_init(class,
list_of_attrs, other_stuff) that would write the code and exec it.
However, this would work better as part of a python development
environment than as part of run-time code.

Terry J. Reedy






More information about the Python-list mailing list