pythonclass

__new__ and __init__ in Python


I am learning Python, and so far I understand the following about __new__ and __init__:

  1. __new__ is for object creation
  2. __init__ is for object initialization
  3. __new__ is invoked before __init__, as __new__ returns a new instance and __init__ is invoked afterwards to initialize inner state.
  4. __new__ is good for immutable objects, as they cannot be modified after creation. So, we can return a new instance with a new state.
  5. We can use __new__ and __init__ for mutable object as well since their inner state can be changed.

But I have other questions now.

  1. When I create a new instance such as a = MyClass("hello","world"), how are these arguments passed? I mean, how I should I structure the class using __init__ and __new__ since they both accept arbitrary arguments beyond the default first argument?
  2. Can the self keyword be changed to something else in name only? Similarly, can cls also be renamed, since it is just a parameter?

I made a little experiments as such below:

>>> class MyClass(tuple):
    def __new__(tuple):
        return [1,2,3]

and then:

>>> a = MyClass()
>>> a
[1, 2, 3]

Even through I said I want to return tuple, this code works fine and returned [1,2,3] (a list). I thought the first parameter to new should be the type of the instance we want to return. We're talking about the new function, right? I'm not familiar with how return types work in other languages in this context, beyond the concept of a bound type.

I also tried this:

>>> issubclass(MyClass,list)
False
>>> issubclass(MyClass,tuple)
True
>>> isinstance(a,MyClass)
False
>>> isinstance(a,tuple)
False
>>> isinstance(a,list)
True

I didn’t conduct further experiments because the results were becoming unclear, so I decided to stop and ask on StackOverflow instead.

The SO posts I read:

  1. Python object creation
  2. Why is __init__() always called after __new__()?

Solution

  • how I should structure the class using __init__ and __new__ as they are different and both accepts arbitrary arguments besides default first argument.

    Only rarely will you have to worry about __new__. Usually, you'll just define __init__ and let the default __new__ pass the constructor arguments to it.

    self keyword is in terms of name can be changed to something else? But I am wondering cls is in terms of name is subject to change to something else as it is just a parameter name?

    Both are just parameter names with no special meaning in the language. But their use is a very strong convention in the Python community; most Pythonistas will never change the names self and cls in these contexts and will be confused when someone else does.

    Note that your use of def __new__(tuple) re-binds the name tuple inside the constructor function. When actually implementing __new__, you'll want to do it as

    def __new__(cls, *args, **kwargs):
        # do allocation to get an object, say, obj
        return obj
    

    Albeit I said I want to return tuple, this code works fine and returned me [1,2,3].

    MyClass() will have the value that __new__ returns. There's no implicit type checking in Python; it's the responsibility of the programmer to return the correct type ("we're all consenting adults here"). Being able to return a different type than requested can be useful for implementing factories: you can return a subclass of the type requested.

    This also explains the issubclass/isinstance behavior you observe: the subclass relationship follows from your use of class MyClass(tuple), the isinstance reflects that you return the "wrong" type from __new__.

    For reference, check out the requirements for __new__ in the Python Language Reference.

    Edit: ok, here's an example of potentially useful use of __new__. The class Eel keeps track of how many eels are alive in the process and refuses to allocate if this exceeds some maximum.

    class Eel(object):
        MAX_EELS = 20
        n_eels = 0
    
        def __new__(cls, *args, **kwargs):
            if cls.n_eels == cls.MAX_EELS:
                raise HovercraftFull()
    
            obj = super(Eel, cls).__new__(cls)
            cls.n_eels += 1
            return obj
    
        def __init__(self, voltage):
            self.voltage = voltage
    
        def __del__(self):
            type(self).n_eels -= 1
    
        def electric(self):
            """Is this an electric eel?"""
            return self.voltage > 0
    

    Mind you, there are smarter ways to accomplish this behavior.