What is a clean "pythonic" way to implement multiple constructors?
Asked Answered
C

16

956

I can't find a definitive answer for this. As far as I know, you can't have multiple __init__ functions in a Python class. So how do I solve this problem?

Suppose I have a class called Cheese with the number_of_holes property. How can I have two ways of creating cheese objects...

  1. One that takes a number of holes like this: parmesan = Cheese(num_holes=15).
  2. And one that takes no arguments and just randomizes the number_of_holes property: gouda = Cheese().

I can think of only one way to do this, but this seems clunky:

class Cheese:
    def __init__(self, num_holes=0):
        if num_holes == 0:
            # Randomize number_of_holes
        else:
            number_of_holes = num_holes

What do you say? Is there another way?

Clyte answered 25/3, 2009 at 17:0 Comment(3)
I think init is not a constructor, it is an initializer. new would be a constructorPasadis
Related (not duplicate): How can I detect duplicate method names in a Python class?Selfabuse
I think that this question could be re-titled, "How can I have default arguments for a class constructor?"Underlaid
R
970

Actually None is much better for "magic" values:

class Cheese:
    def __init__(self, num_holes=None):
        if num_holes is None:
            ...

Now if you want complete freedom of adding more parameters:

class Cheese:
    def __init__(self, *args, **kwargs):
        # args -- tuple of anonymous arguments
        # kwargs -- dictionary of named arguments
        self.num_holes = kwargs.get('num_holes', random_holes())

To better explain the concept of *args and **kwargs (you can actually change these names):

def f(*args, **kwargs):
   print('args:', args, 'kwargs:', kwargs)

>>> f('a')
args: ('a',) kwargs: {}
>>> f(ar='a')
args: () kwargs: {'ar': 'a'}
>>> f(1,2,param=3)
args: (1, 2) kwargs: {'param': 3}

http://docs.python.org/reference/expressions.html#calls

Refuel answered 25/3, 2009 at 17:3 Comment(8)
For those interested, kwargs stands for keyword arguments (seems logic once you know it). :)Age
There are moments that *args and **kwargs are an overkill. At most constructors, you want to know what your arguments are.Wheeze
@Wheeze Yes! For sure!Furgeson
@Wheeze Yeah, this approach is not self-documenting at all (how many times have you tried to use a library and tried to intuited the usage from method signatures only to discover you have to do a code dive to see what arguments are expected/allowed?) Moreover, now your implementation takes on the added burden of argument checking, including the choice of whether to accept or except (teehee) unsupported arguments.Marikomaril
For folks from google in 2020, scroll down this page a bit - the answer by 'Ber' further down is solid and more pythonic than this route for most scenarios.Lomalomas
@Wheeze same issue in R's documentation. So many functions with elipsis '...' arguments. Extends those trips to the docs...Squeegee
that breaks intellisense right ?Teenateenage
@TomH and for folks from google in 2024, scroll back up again because that answer has now surpassed this one in score!Oireachtas
P
972

Using num_holes=None as the default is fine if you are going to have just __init__.

If you want multiple, independent "constructors", you can provide these as class methods. These are usually called factory methods. In this case you could have the default for num_holes be 0.

class Cheese(object):
    def __init__(self, num_holes=0):
        "defaults to a solid cheese"
        self.number_of_holes = num_holes

    @classmethod
    def random(cls):
        return cls(randint(0, 100))

    @classmethod
    def slightly_holey(cls):
        return cls(randint(0, 33))

    @classmethod
    def very_holey(cls):
        return cls(randint(66, 100))

Now create object like this:

gouda = Cheese()
emmentaler = Cheese.random()
leerdammer = Cheese.slightly_holey()
Prodrome answered 25/3, 2009 at 17:11 Comment(22)
+1 for the nice example of @classmethod. But as answer to the original question I prefer the accepted solution, because in my opinion it is more in the direction of having multiple constructors (or overloading them, in other languages).Halitosis
@rmbianchi: The accepted answer may be more in line with other languages, but it is also less pythonic: @classmethods are the pythonic way of implementing multiple contstructors.Yaekoyael
@EthanFurman: I like your cheese and thanks for the improved code. This is really better, using the __init__() constructor directly.Prodrome
Ingenious. So the class methods are static?Hastings
@Bepetersn There are instance methods (the normal ones), which have an instance object referenced as self. Then there are class methods (using @classmethod) which have a reference to the class object as cls. An finally there are static methods (declared with @staticmethod) which have neither of those references. Static methods are just like functions at module level, except they live in the class' name space.Prodrome
An advantage of this method over the accepted solution is that it easily allows to specify abstract constructors and enforce implementation of them, especially with python 3 in which the usage of @abstractmethod and @classmethod on the same factory function is possible and is built into the language. I would also argue that this approach is more explicit, which goes with The Zen of Python.Loaded
That means: Instead of simply implementing one constructor for every specific construction required I need to implement a class method. This method then redirects to a single constructor where I then need to have a great variety of ifs to distinguish between the different ways of construction. One of the commentators wrote: "Simply beautiful." Sorry, but I really don't know if this approach is more beautiful than having multiple constructors.Karmakarmadharaya
@RegisMay don't forget that in Python, there is only one __init__(self) method per class, so you cannot have more than one constructor in a class.Prodrome
That is exactly what I am taking about: Because of that a more complex implementation is required.Karmakarmadharaya
A great example of this is in GeoPandas: github.com/geopandas/geopandas/blob/master/geopandas/…Powell
In the init method defined above, it is clear that it is held in 'number_of_holes' -- not so for the @classmethod 'constructors'. Which data member holds the number of holes? .Edile
@ashu The other constructors call the __init__() method by instantiating the class via cls(...). Therefore, the number_of_holes is always used in the same way.Prodrome
This answer was very helpful but I refuse to upvote past 666Clematis
@RegisMay (1/2) Rather than having a bunch of ifs in __init__(), the trick is to have each of the unique factory methods handle their own unique aspects of initialization, and have __init__() accept only the fundamental pieces of data that define an instance. For example, Cheese might have attributes volume and average_hole_radius in addition to number_of_holes. __init__() would accept these three values. Then you could have a class method with_density() that randomly chooses the fundamental attributes to match a given density, subsequently passing them on to __init__().Teepee
(2/2) Python also lets us make properties, attributes which are resolved when accessed. So you could likewise have a density property that's calculated from the fundamental attributes. The result is separation of concerns by having __init__() handle the raw data while any number of other factory methods/properties are permitted to handle various usage scenarios. I imagine this is why some might be commenting, "Simply beautiful."Teepee
@NathanielJones Yes, I know, that's the detour you have to take. And find an individual name for every single way of construction though all the factory methods do exactly the same. Which is not "simply beautiful". Especially as I can not force the user to use the factory methods only. It might be something - but not "simply beautiful".Karmakarmadharaya
This is very not flexible and not what in programming is most commonly meant by "multiple constructor". This technique uses the __init__ function anyway, and it is bound to its argument. This is not possible, for example, despite being a very common use case: def foo(property_different_from_holes)Sexagesimal
@EduardoPignatelli You can add more parameters to __init__() and provide suitable default vales, as in def __init__(self, num_holes=0, color='cheezy_yellow'): which will allow you to define like @classmethod def colored(cls, color): return cls(color=color)Prodrome
@Prodrome Yeah, that's the pattern I am using right know, but, still, it is a workaround, not a proper solutions like you would find in .NET, or C++.Sexagesimal
Am I right in thinking that a @classmethod function calls the __new__ function for the type, and then calls __init__ with the arguments specified by the implementation details of each @classmethod function? In other words, random() first calls Cheese.__new__() and then calls Cheese.__init__(value) where value is set to whatever is produced by the call to randint?Foreshow
I'm not really sure that I agree with this design, because the different constructor functions are all calling the same thing: __init__. This is a bad design, because it forces __init__ to become one gargantuan function which takes all possible combinations of arguments and has to include lots of complex logic to sort them all out. In the more general case, one will have a class type which can take multiple different sets of arguments, which might be exclusive of each other in complex ways. A better design would be for __init__ to initialze everything to None and then have each...Foreshow
A better design would be for __init__ to initialze everything to None and then have each constructor function over-write some of those values depending on the required logic.Foreshow
R
970

Actually None is much better for "magic" values:

class Cheese:
    def __init__(self, num_holes=None):
        if num_holes is None:
            ...

Now if you want complete freedom of adding more parameters:

class Cheese:
    def __init__(self, *args, **kwargs):
        # args -- tuple of anonymous arguments
        # kwargs -- dictionary of named arguments
        self.num_holes = kwargs.get('num_holes', random_holes())

To better explain the concept of *args and **kwargs (you can actually change these names):

def f(*args, **kwargs):
   print('args:', args, 'kwargs:', kwargs)

>>> f('a')
args: ('a',) kwargs: {}
>>> f(ar='a')
args: () kwargs: {'ar': 'a'}
>>> f(1,2,param=3)
args: (1, 2) kwargs: {'param': 3}

http://docs.python.org/reference/expressions.html#calls

Refuel answered 25/3, 2009 at 17:3 Comment(8)
For those interested, kwargs stands for keyword arguments (seems logic once you know it). :)Age
There are moments that *args and **kwargs are an overkill. At most constructors, you want to know what your arguments are.Wheeze
@Wheeze Yes! For sure!Furgeson
@Wheeze Yeah, this approach is not self-documenting at all (how many times have you tried to use a library and tried to intuited the usage from method signatures only to discover you have to do a code dive to see what arguments are expected/allowed?) Moreover, now your implementation takes on the added burden of argument checking, including the choice of whether to accept or except (teehee) unsupported arguments.Marikomaril
For folks from google in 2020, scroll down this page a bit - the answer by 'Ber' further down is solid and more pythonic than this route for most scenarios.Lomalomas
@Wheeze same issue in R's documentation. So many functions with elipsis '...' arguments. Extends those trips to the docs...Squeegee
that breaks intellisense right ?Teenateenage
@TomH and for folks from google in 2024, scroll back up again because that answer has now surpassed this one in score!Oireachtas
R
77

One should definitely prefer the solutions already posted, but since no one mentioned this solution yet, I think it is worth mentioning for completeness.

The @classmethod approach can be modified to provide an alternative constructor which does not invoke the default constructor (__init__). Instead, an instance is created using __new__.

This could be used if the type of initialization cannot be selected based on the type of the constructor argument, and the constructors do not share code.

Example:

class MyClass(set):

    def __init__(self, filename):
        self._value = load_from_file(filename)

    @classmethod
    def from_somewhere(cls, somename):
        obj = cls.__new__(cls)  # Does not call __init__
        super(MyClass, obj).__init__()  # Don't forget to call any polymorphic base class initializers
        obj._value = load_from_somewhere(somename)
        return obj
Reset answered 11/8, 2016 at 0:20 Comment(3)
This is the solution that indeed provides independent constructors instead of fiddling with __init__'s arguments. However, could you provide some references, please, that this method is somehow officially approved or supported? How safe and reliable is it to call directly __new__ method?Ingles
I did things this way and then came here to ask the above question to see if my way was right. You still need to call super otherwise this won't work in cooperative multiple inheritance, so I added the line to your answer.Archives
I wonder if one could define a decorator 'constructor' (that wraps up the new and super stuff) and then do: @constructor def other_init(self, stuff): self.stuff = stuffShigella
S
31

All of these answers are excellent if you want to use optional parameters, but another Pythonic possibility is to use a classmethod to generate a factory-style pseudo-constructor:

def __init__(self, num_holes):

  # do stuff with the number

@classmethod
def fromRandom(cls):

  return cls( # some-random-number )
Swop answered 25/3, 2009 at 17:16 Comment(0)
G
21

Why do you think your solution is "clunky"? Personally I would prefer one constructor with default values over multiple overloaded constructors in situations like yours (Python does not support method overloading anyway):

def __init__(self, num_holes=None):
    if num_holes is None:
        # Construct a gouda
    else:
        # custom cheese
    # common initialization

For really complex cases with lots of different constructors, it might be cleaner to use different factory functions instead:

@classmethod
def create_gouda(cls):
    c = Cheese()
    # ...
    return c

@classmethod
def create_cheddar(cls):
    # ...

In your cheese example you might want to use a Gouda subclass of Cheese though...

Genovera answered 25/3, 2009 at 17:11 Comment(1)
Factory functions use cls: use cls instead of Cheese. If not, what is the point of using class methods instead of static methods?Needleful
T
19

Those are good ideas for your implementation, but if you are presenting a cheese making interface to a user. They don't care how many holes the cheese has or what internals go into making cheese. The user of your code just wants "gouda" or "parmesean" right?

So why not do this:

# cheese_user.py
from cheeses import make_gouda, make_parmesean

gouda = make_gouda()
paremesean = make_parmesean()

And then you can use any of the methods above to actually implement the functions:

# cheeses.py
class Cheese(object):
    def __init__(self, *args, **kwargs):
        #args -- tuple of anonymous arguments
        #kwargs -- dictionary of named arguments
        self.num_holes = kwargs.get('num_holes',random_holes())

def make_gouda():
    return Cheese()

def make_paremesean():
    return Cheese(num_holes=15)

This is a good encapsulation technique, and I think it is more Pythonic. To me this way of doing things fits more in line more with duck typing. You are simply asking for a gouda object and you don't really care what class it is.

Thinking answered 25/10, 2011 at 15:6 Comment(2)
I tend to opt for this approach because it is remarkably similar to the Factory Method pattern.Aspic
make_gouda, make_parmesan should be classmethods of class CheesePlast
S
15

Overview

For the specific cheese example, I agree with many of the other answers about using default values to signal random initialization or to use a static factory method. However, there may also be related scenarios that you had in mind where there is value in having alternative, concise ways of calling the constructor without hurting the quality of parameter names or type information.

Since Python 3.8 and functools.singledispatchmethod can help accomplish this in many cases (and the more flexible multimethod can apply in even more scenarios). (This related post describes how one could accomplish the same in Python 3.4 without a library.) I haven't seen examples in the documentation for either of these that specifically shows overloading __init__ as you ask about, but it appears that the same principles for overloading any member method apply (as shown below).

"Single dispatch" (available in the standard library) requires that there be at least one positional parameter and that the type of the first argument be sufficient to distinguish among the possible overloaded options. For the specific Cheese example, this doesn't hold since you wanted random holes when no parameters were given, but multidispatch does support the very same syntax and can be used as long as each method version can be distinguish based on the number and type of all arguments together.

Example

Here is an example of how to use either method (some of the details are in order to please mypy which was my goal when I first put this together):

from functools import singledispatchmethod as overload
# or the following more flexible method after `pip install multimethod`
# from multimethod import multidispatch as overload


class MyClass:

    @overload  # type: ignore[misc]
    def __init__(self, a: int = 0, b: str = 'default'):
        self.a = a
        self.b = b

    @__init__.register
    def _from_str(self, b: str, a: int = 0):
        self.__init__(a, b)  # type: ignore[misc]

    def __repr__(self) -> str:
        return f"({self.a}, {self.b})"


print([
    MyClass(1, "test"),
    MyClass("test", 1),
    MyClass("test"),
    MyClass(1, b="test"),
    MyClass("test", a=1),
    MyClass("test"),
    MyClass(1),
    # MyClass(),  # `multidispatch` version handles these 3, too.
    # MyClass(a=1, b="test"),
    # MyClass(b="test", a=1),
])

Output:

[(1, test), (1, test), (0, test), (1, test), (1, test), (0, test), (1, default)]

Notes:

  • I wouldn't usually make the alias called overload, but it helped make the diff between using the two methods just a matter of which import you use.
  • The # type: ignore[misc] comments are not necessary to run, but I put them in there to please mypy which doesn't like decorating __init__ nor calling __init__ directly.
  • If you are new to the decorator syntax, realize that putting @overload before the definition of __init__ is just sugar for __init__ = overload(the original definition of __init__). In this case, overload is a class so the resulting __init__ is an object that has a __call__ method so that it looks like a function but that also has a .register method which is being called later to add another overloaded version of __init__. This is a bit messy, but it please mypy becuase there are no method names being defined twice. If you don't care about mypy and are planning to use the external library anyway, multimethod also has simpler alternative ways of specifying overloaded versions.
  • Defining __repr__ is simply there to make the printed output meaningful (you don't need it in general).
  • Notice that multidispatch is able to handle three additional input combinations that don't have any positional parameters.
Supply answered 10/6, 2021 at 19:40 Comment(1)
Thank you for this answer and the reference to multimethod package. In some situations multiple dispatch just feels so natural. Having worked in Julia for a while, it is something I miss in Python.Travax
P
10

Use num_holes=None as a default, instead. Then check for whether num_holes is None, and if so, randomize. That's what I generally see, anyway.

More radically different construction methods may warrant a classmethod that returns an instance of cls.

Paleoclimatology answered 25/3, 2009 at 17:3 Comment(1)
Is "classmethod" literal? Or do you mean class method?Selfabuse
B
9

The best answer is the one above about default arguments, but I had fun writing this, and it certainly does fit the bill for "multiple constructors". Use at your own risk.

What about the new method.

"Typical implementations create a new instance of the class by invoking the superclass’s new() method using super(currentclass, cls).new(cls[, ...]) with appropriate arguments and then modifying the newly-created instance as necessary before returning it."

So you can have the new method modify your class definition by attaching the appropriate constructor method.

class Cheese(object):
    def __new__(cls, *args, **kwargs):

        obj = super(Cheese, cls).__new__(cls)
        num_holes = kwargs.get('num_holes', random_holes())

        if num_holes == 0:
            cls.__init__ = cls.foomethod
        else:
            cls.__init__ = cls.barmethod

        return obj

    def foomethod(self, *args, **kwargs):
        print "foomethod called as __init__ for Cheese"

    def barmethod(self, *args, **kwargs):
        print "barmethod called as __init__ for Cheese"

if __name__ == "__main__":
    parm = Cheese(num_holes=5)
Biysk answered 25/3, 2009 at 19:48 Comment(4)
This is the sort of code that gives me nightmares about working in dynamic languages--not to say that there's anything inherently wrong with it, only that it violates some key assumptions I would make about a class.Swop
@javawizard Would it be easy to explain in a comment what makes it non thread-safe, or give a pointer so I can read about it somewhere else?Guggle
@Guggle Say two threads try to create cheeses at the same time, one with Cheese(0) and one with Cheese(1). It's possible that thread 1 might run cls.__init__ = cls.foomethod, but then thread 2 might run cls.__init__ = cls.barmethod before thread 1 gets any further. Both threads will then end up calling barmethod, which isn't what you want.Flodden
Indeed, there is no reason to modify the definition of the class just to handle creation of one instance of the class.Lugo
G
4

I'd use inheritance. Especially if there are going to be more differences than number of holes. Especially if Gouda will need to have different set of members then Parmesan.

class Gouda(Cheese):
    def __init__(self):
        super(Gouda).__init__(num_holes=10)


class Parmesan(Cheese):
    def __init__(self):
        super(Parmesan).__init__(num_holes=15) 
Gerri answered 28/4, 2015 at 12:33 Comment(1)
Inheritance might be appropriate, but it's really an orthogonal issue to what is being asked.Lugo
B
3

This is how I solved it for a YearQuarter class I had to create. I created an __init__ which is very tolerant to a wide variety of input.

You use it like this:

>>> from datetime import date
>>> temp1 = YearQuarter(year=2017, month=12)
>>> print temp1
2017-Q4
>>> temp2 = YearQuarter(temp1)
>>> print temp2
2017-Q4
>>> temp3 = YearQuarter((2017, 6))
>>> print temp3
2017-Q2 
>>> temp4 = YearQuarter(date(2017, 1, 18))
>>> print temp4
2017-Q1
>>> temp5 = YearQuarter(year=2017, quarter = 3)
>>> print temp5
2017-Q3

And this is how the __init__ and the rest of the class looks like:

import datetime


class YearQuarter:

    def __init__(self, *args, **kwargs):
        if len(args) == 1:
            [x]     = args

            if isinstance(x, datetime.date):
                self._year      = int(x.year)
                self._quarter   = (int(x.month) + 2) / 3
            elif isinstance(x, tuple):
                year, month     = x

                self._year      = int(year)

                month           = int(month)

                if 1 <= month <= 12:
                    self._quarter   = (month + 2) / 3
                else:
                    raise ValueError

            elif isinstance(x, YearQuarter):
                self._year      = x._year
                self._quarter   = x._quarter

        elif len(args) == 2:
            year, month     = args

            self._year      = int(year)

            month           = int(month)

            if 1 <= month <= 12:
                self._quarter   = (month + 2) / 3
            else:
                raise ValueError

        elif kwargs:

            self._year      = int(kwargs["year"])

            if "quarter" in kwargs:
                quarter     = int(kwargs["quarter"])

                if 1 <= quarter <= 4:
                    self._quarter     = quarter
                else:
                    raise ValueError
            elif "month" in kwargs:
                month   = int(kwargs["month"])

                if 1 <= month <= 12:
                    self._quarter     = (month + 2) / 3
                else:
                    raise ValueError

    def __str__(self):
        return '{0}-Q{1}'.format(self._year, self._quarter)
Bibb answered 18/1, 2017 at 12:17 Comment(7)
I have used this effectively but with classes of my own instead of Python types. Given __init__(self, obj) I test inside __init__ with if str(obj.__class__.__name__) == 'NameOfMyClass': ... elif etc..Doc
This really isn't very Pythonic. __init__ should take a year and a quarter directly, rather than a single value of unknown type. A class method from_date can handle extracting a year and quarter from a datetime.date value, then calling YearQuarter(y, q). You could define a similar class method from_tuple, but that hardly seems worth doing since you could simply call YearQuarter(*t).Lugo
@Lugo I gave it a huge update. Please tell me what you think.Bibb
It's still a mess (even more so than before) of special cases. __init__ shouldn't responsible for analyzing every possible set of values you might use to create an instance. def __init__(self, year, quarter): self._year = year; self._quarter = quarter: that's it (though may be with some range checking on quarter). Other class methods handle the job of mapping a different argument or arguments to a year and a quarter that can be passed to __init__.Lugo
For example, from_year_month takes a month m, maps it to a quarter q, then calls YearQuarter(y, q). from_date extracts the year and the month from the date instance, then calls YearQuarter._from_year_month. No repetition, and each method is responsible for one specific way of generating a year and a quarter to pass to __init__.Lugo
@Lugo thanks for your feedback but I still strongly believe this is for users of the class (not the creator) a pleasant and Pythonic way of using it.Bibb
This is for the user as well. Class methods provide individual methods with self-documenting names, rather than a single entry point that could do any number of things.Lugo
I
3

Since my initial answer was criticised on the basis that my special-purpose constructors did not call the (unique) default constructor, I post here a modified version that honours the wishes that all constructors shall call the default one:

class Cheese:
    def __init__(self, *args, _initialiser="_default_init", **kwargs):
        """A multi-initialiser.
        """
        getattr(self, _initialiser)(*args, **kwargs)

    def _default_init(self, ...):
        """A user-friendly smart or general-purpose initialiser.
        """
        ...

    def _init_parmesan(self, ...):
        """A special initialiser for Parmesan cheese.
        """
        ...

    def _init_gouda(self, ...):
        """A special initialiser for Gouda cheese.
        """
        ...

    @classmethod
    def make_parmesan(cls, *args, **kwargs):
        return cls(*args, **kwargs, _initialiser="_init_parmesan")

    @classmethod
    def make_gouda(cls, *args, **kwargs):
        return cls(*args, **kwargs, _initialiser="_init_gouda")
Ingles answered 14/2, 2019 at 14:11 Comment(1)
The idea of a class method is to separate creating a special instance into two independent pieces: first, you define a generic __init__ that can handle initializing Cheese without having to know about special kinds of cheeses. Second, you define a class method that generates the appropriate arguments to the generic __init__ for certain special cases. Here, you are basically reinventing parts of inheritance.Lugo
I
1
class Cheese:
    def __init__(self, *args, **kwargs):
        """A user-friendly initialiser for the general-purpose constructor.
        """
        ...

    def _init_parmesan(self, *args, **kwargs):
        """A special initialiser for Parmesan cheese.
        """
        ...

    def _init_gauda(self, *args, **kwargs):
        """A special initialiser for Gauda cheese.
        """
        ...

    @classmethod
    def make_parmesan(cls, *args, **kwargs):
        new = cls.__new__(cls)
        new._init_parmesan(*args, **kwargs)
        return new

    @classmethod
    def make_gauda(cls, *args, **kwargs):
        new = cls.__new__(cls)
        new._init_gauda(*args, **kwargs)
        return new
Ingles answered 25/2, 2018 at 21:52 Comment(11)
No. This is utterly unPythonic, it's like Java masquerading behind Python syntax. You want one single __init__ method, and the other class methods either call it as-is (cleanest) or handle special initialization actions via any helper classmethods and setters you need (ideally none).Plast
I do not want a single __init__ method when I have multiple constructors with different initialisation routines. I do not see why someone would want it. "the other class methods either call it as-is" -- call what? The __init__ method? That would be strange to call __init__ explicitely IMO.Ingles
Alexey, it is utterly unPythonic to have multiple constructors, as in multiple _init... methods (see other answers on this question.) Worse still, in this case you don't even need to: you haven't shown how the code for _init_parmesan, _init_gouda differ, so there is zero reason not to common-case them. Anyway, the Pythonic way to do that is to supply non-default args to *args or **kwargs (e.g. Cheese(..., type='gouda'...), or if that can't handle everything, put the general code in __init__ and the less-commonly-used code in a classmethod make_whatever... and have it cal settersPlast
"it is utterly unPythonic to have multiple constructors" -- the original question is still "What is a clean, pythonic way to have multiple constructors in Python?". I only showed how to have them, not why i would want them.Ingles
As to the question why i would want them, well, even namedtuple-produced classes have two constructors: the default one and _make. There are other examples when inheriting from built-in classes where using multiple constructors would be the only option, because despatching inside __init__ would not be an option.Ingles
Even when multiple initialisation routines can be achieved with the single default constructor by some (possibly awkward) dispatch inside __init__, if the routines are completely independent, i will call them _init_from_foo, _init_from_bar, etc, and call them from __init__ after dispatching by isinstance or by other tests.Ingles
@smci, I still do not understand your original comment: did you suggest to call __init__ explicitly from some class methods? (That would be a bit strange IMO.)Ingles
No. The standard Python way for about a decade is for the non-default make_... or from_... methods to be classmethods which call Cheese(), and thus implicitly its __init__ method, possibly with non-default args. They then return the new Cheese() instance, modified as necessary. This is what I've been saying.Plast
@smci, I think i understand your point now, thanks for the explanation. I see a certain uniformity in such approach. I am not convinced yet though that the restriction of having to always pass through the default constructor (FooClass(...)) is not artificial. In particular, i wonder if Python classes realised in C (like NumPy classes) respect that restriction.Ingles
no idea about classes implemented in C, you might ask that as a separate question. I imagine there's a nuance for wrapped C classes that can also be directly instantiated from C without thunking back in and out of Python.Plast
I really like this solution. It has the noted advantage of letting you add initialization methods to a pre-existing class that already exists while keeping reasonably clean code. In theory, it's nice to have an inner constructor that everything goes through, but in practice sometimes the existing __init__'s API sucks.Revulsion
C
1

I do not see a straightforward answer with an example yet. The idea is simple:

  • use __init__ as the "basic" constructor as python only allows one __init__ method
  • use @classmethod to create any other constructors and call the basic constructor

Here is a new try.

 class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    @classmethod
    def fromBirthYear(cls, name, birthYear):
        return cls(name, date.today().year - birthYear)

Usage:

p = Person('tim', age=18)
p = Person.fromBirthYear('tim', birthYear=2004)
Czechoslovak answered 6/5, 2022 at 19:34 Comment(0)
S
0

Here (drawing on this earlier answer, the pure Python version of classmethod in the docs, and as suggested by this comment) is a decorator that can be used to create multiple constructors.

from types import MethodType
from functools import wraps

class constructor:
    def __init__(self, func):

        @wraps(func)                      
        def wrapped(cls, *args, **kwargs):
            obj = cls.__new__(cls)        # Create new instance but don't init
            super(cls, obj).__init__()    # Init any classes it inherits from
            func(obj, *args, **kwargs)    # Run the constructor with obj as self
            return obj                
        
        self.wrapped = wrapped

    def __get__(self, _, cls):
        return MethodType(self.wrapped, cls)   # Bind this constructor to the class 
        
    
class Test:
    def __init__(self, data_sequence):
        """ Default constructor, initiates with data sequence """
        self.data = [item ** 2 for item in data_sequence]
        
    @constructor
    def zeros(self, size):
        """ Initiates with zeros """
        self.data = [0 for _ in range(size)]
           
a = Test([1,2,3])
b = Test.zeros(100)

This seems the cleanest way in some cases (see e.g. multiple dataframe constructors in Pandas), where providing multiple optional arguments to a single constructor would be inconvenient: for example cases where it would require too many parameters, be unreadable, be slower or use more memory than needed. However, as earlier comments have argued, in most cases it is probably more Pythonic to route through a single constructor with optional parameters, adding class methods where needed.

Steiner answered 5/1, 2023 at 12:12 Comment(0)
F
0

I don't think any of the answers here get it quite right, although some come close.

Many answers suggest something like the following:

  • Provide a "most general" __init__ function, which takes all possible arguments
  • __init__ should (in general) have some complex logic to check all the arguments for consistency, and then set member data depending on those arguments
  • Other "constructor functions" should have more specific combinations of arguments, and all of them should call __init__

I think this is the wrong design. Unfortunatly, the example given by OP is too simple to fully show why this is a bad design, as in this case the "cheese" type only takes a single integer value in all cases.

In order to realize why it is bad, we need to see a more complex example.

This is from something I am working on:

Using the above paradim this is what we end up writing:

class ExperimentRecord():

    def __init__(self, experiment_index=None, dictionary=None):

        if experiment_index is None and dictionary is None:
            raise ExperimentalDatabaseException(f'constructing instance of ExperimentalRecord requires either experiment_index or dictionary to be specified')
        elif experiment_index is not None and dictionary is not None:
            raise ExperimentalDatabaseException(f'constructing instance of ExperimentalRecoed requires either experiment_index or dictionary to be specified, but not both')
        elif experiment_index is None and dictionary is not None:
            self.experiment_index = dictionary['index']
            self.record_type = dictionary['record_type']
            self.data = dictionary['data']
            self.measurement_amplitude = dictionary['amplitude']
            self.measurement_mean = dictionary['mean']
            self.measurement_stddev = dictionary['stddev']
            self.measurement_log_likelihood = dictionary['log_likelihood']
        elif experiment_index is not None and dictionary is None:
            self.experiment_index = experiment_index
            self.record_type = None
            self.data = None
            self.measurement_amplitude = None
            self.measurement_mean = None
            self.measurement_stddev = None
            self.measurement_log_likelihood = None

The resulting code is, to put it bluntly (and I say this as the person who wrote this code), shockingly bad. These are the reasons why:

  • __init__ has to use complex combinatorial logic to validate the arguments
  • if the arguments form a valid combination, then it performs some extensive initialization, in the same function
  • this violates the single responsible principle and leads to complex code which is hard to maintain, or even understand
  • it can be improved by adding two functions __init_from_dictionary and __init_from_experimental_index but this leads to extra functions being added for really no purpose other than to try and keep the __init__ function managable
  • this is totally not how multiple constructors work in languages like Java, C++ or even Rust. Typically we expect function overloading to seperate out the logic for different ways of initializing something into totally independent functions. Here, we mixed everything into a single function, which is the exact opposite of what we want to achieve

Further, in this example, the initialization is dependent only on two variables. But I could have easily added a third:

  • For example, we might want to initialize an experimental record from a string or even a filename/path or file handle
  • We can imagine that the complexity explodes as more possible methods of initialization are introduced
  • In more complex cases, each argument might not be independent. We could imagine possible cases for initialization where valid initializations are formed from a subset of possible arguments, where the subsets overlap somehow in a complex way

For example:

Some object might take arguments A, B, C, D, E. It might be valid to initialize using the following combinations:

  • A
  • B, C, D
  • D, E
  • A, E

This is an abstract example, because it is hard to think of an simple example to present. However, if you have been around a while in the field of software engineering, you will know that such examples can and do sometimes arrise, regardless of whether their existance points to some shortcominings in the overall design.


With the above said, this is what I am working with, right now. It probably isn't perfect, I have only just started working with Python in a context which required me to write "multiple constructors" as of yesterday.

We fix the problems by doing the following:

  • make __init__ a "null" constructor. It should do the work of a constructor which takes no arguments
  • Add constructor functions which modify the object in some way after calling the null constructor (__init__)
  • Or, if the use case lends itself to inheritance, use an inheritance pattern as others have suggested. (This may or may not be "better" depending on the context)

Something like this, maybe

class ExperimentRecord():

    def __init__():
        self.experiment_index = None
        self.record_type = None
        self.data = None
        self.measurement_amplitude = None
        self.measurement_mean = None
        self.measurement_stddev = None
        self.measurement_log_likelihood = None

    @classmethod
    def from_experiment_index(cls, experiment_index):
        tmp = cls() # calls `__new__`, `__init__`, unless I misunderstand
        tmp.experiment_index = experiment_index
        return tmp

    @classmethod
    def from_dictionary(cls, dictionary):
        tmp = cls()
        tmp .experiment_index = dictionary['index']
        tmp .record_type = dictionary['record_type']
        tmp .data = dictionary['data']
        tmp .measurement_amplitude = dictionary['amplitude']
        tmp .measurement_mean = dictionary['mean']
        tmp .measurement_stddev = dictionary['stddev']
        tmp .measurement_log_likelihood = dictionary['log_likelihood']
        return tmp

With this design, we solve the following problems:

  • single responsibility principle: each constructor function is fully independent and does its own thing to initialize the object
  • each constructor function takes the arguments it requires for initialization, and nothing more. each possible method of initization requires its own set of arguments, and those sets of arguments are indepenent, and not mashed into one single function call

Note: Since I literally just thought of this, it's possible I have overlooked something. If that is the case please leave a comment explaining the deficiencies and I will try and think of a resolution, and then update the answer. This seems to work for my particular use case but there is always a possibility I have overlooked something, particularly as I didn't know have any need to investigate writing multiple Python constructors until today.

Foreshow answered 13/11, 2023 at 14:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.