[Python-ideas] object construction (was: Re: ML Style Pattern Matching for Python)
Mart Sõmermaa
mrts.pydev at gmail.com
Sat Jan 1 13:57:12 CET 2011
On Sun, Dec 19, 2010 at 9:44 PM, spir <denis.spir at gmail.com> wrote:
> I find those loads of "self.x=x" in constructors sooo stupid --I want the machine to do it for me. __init__ should only define the essential part of obj construction; while the final constructor would do some mechanical job in addition.
Automating that is quite easy with keyword arguments:
>>> class Foo(object):
... def __init__(self, **kwargs):
... self.__dict__.update(kwargs)
...
>>> f = Foo(a=1, b=2)
>>> f.a
1
>>> f.b
2
If you want to play safe, filter out keys that start with '__'.
Best regards and a happy new year!
Mart Sõmermaa
More information about the Python-ideas
mailing list