newbie: constructor question
Greg Krohn ("X", "@")
"gkrohnXvolucris.8m.com".replace
Mon Oct 14 23:31:40 EDT 2002
More information about the Python-list mailing list
Mon Oct 14 23:31:40 EDT 2002
- Previous message (by thread): File locking question
- Next message (by thread): newbie: constructor question
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
"Alexander Eisenhuth" <stacom at stacom-software.de> wrote in message news:3DAAF148.3060509 at stacom-software.de... > Hallo everybody, > > is it possible to implement multiple constructors with different no. of arguments for one class ? > > >>> class A: > def __init__(self, a): > self._a = a > def __init__(self,a,b): > self._b = b > def __init__(self,a,b,c): > self._c =c > > >>>obj = A(12) > > Traceback (most recent call last): > File "<pyshell#2>", line 1, in ? > obj = A(12) > TypeError: __init__() takes exactly 4 arguments (2 given) > >>> > > -------------------------------- > That doesn't work. Any suggestions ? > > Thanks in forward > > Alexander Depending on what you are actually trying to accomplish, this might work: class ClassA: def __init__(self, **kwargs): for kw in kwargs: setattr(self, kw, kwargs[kw]) Use it like this: >>> objA = ClassA(a=12) >>> print objA.a 12 >>> objB = ClassA(a=12, b='Python') >>> print objB.a, objB.b 12 Python Disclaimer: If you are simply wondering about method overloading, than ignore this post. If you are wondering about creating attributes dynamically on initialization, than this post might help.
- Previous message (by thread): File locking question
- Next message (by thread): newbie: constructor question
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list