More __init__ methods

Marc 'BlackJack' Rintsch bj_666 at
Thu Nov 6 16:59:51 CET 2008

On Thu, 06 Nov 2008 16:49:08 +0100, Mr.SpOOn wrote:

> I know there can be only one __init__ method (at least, I think).
> Often I need an object to be created in different ways, for example
> passing a string as argument, or an integer, or another object. To
> achieve this I put the default value of the arguments to None and then I
> some if...elif inside the __init__.
> Is this a good practice? It actually works, but sometimes I think that
> in this way the __init__ method can become too complicated, for example
> when an object can be created using more than one argument and in
> different combinations.

I'm usually using `classmethod()`\s as alternative "constructors" then. 
For example:

class A(object):
    def __init__(self, a, b, c):
        self.a = a
        # ...

    def from_string(cls, s):
        # ...
        return cls(a, b, c)

	Marc 'BlackJack' Rintsch

More information about the Python-list mailing list