713

I'm just trying to streamline one of my classes and have introduced some functionality in the same style as theflyweight design pattern.

However, I'm a bit confused as to why__init__ is always called after__new__. I wasn't expecting this. Can anyone tell me why this is happening and how I can implement this functionality otherwise? (Apart from putting the implementation into the__new__ which feels quite hacky.)

Here's an example:

class A(object):    _dict = dict()    def __new__(cls):        if 'key' in A._dict:            print "EXISTS"            return A._dict['key']        else:            print "NEW"            return super(A, cls).__new__(cls)    def __init__(self):        print "INIT"        A._dict['key'] = self        print ""a1 = A()a2 = A()a3 = A()

Outputs:

NEWINITEXISTSINITEXISTSINIT

Why?

Martijn Pieters's user avatar
Martijn Pieters
1.1m326 gold badges4.2k silver badges3.4k bronze badges
askedMar 23, 2009 at 17:13
Dan's user avatar
2
  • 1
    was trying to understand design pattern as well, and first time heard of :flyweight design pattern.. and very good link having example in almost all popular langugages.CommentedMay 7, 2020 at 22:10
  • 5
    isn't it a singleton?CommentedMar 13, 2021 at 19:07

20 Answers20

741

Use__new__ when you need to controlthe creation of a new instance.

Use__init__ when you need to control initialization of a new instance.

__new__ is the first step of instance creation. It's called first, and isresponsible for returning a newinstance of your class.

In contrast,__init__ doesn't return anything; it's only responsible for initializing theinstance after it's been created.

In general, you shouldn't need tooverride__new__ unless you'resubclassing an immutable type likestr, int, unicode or tuple.

From April 2008 post:When to use__new__ vs.__init__? on mail.python.org.

You should consider that what you are trying to do is usually done with aFactory and that's the best way to do it. Using__new__ is not a good clean solution so please consider the usage of a factory. Here's a good example:ActiveState Fᴀᴄᴛᴏʀʏ ᴘᴀᴛᴛᴇʀɴ Recipe.

Sign up to request clarification or add additional context in comments.

5 Comments

Ended up using__new__ inside a Factory class, which has become quite clean, so thanks for your input.
Sorry, I disagree that the use of__new__ should be strictly limited to the cases stated. I've found it very useful for implementing extensible generic class factories -- seemy answer to the questionImproper use of__new__ to generate classes in Python? for an example of doing so.
I strongly disagree with__new__ being a bad solution here. The factory pattern is necessary in languages that restrict constructors to act as initializers (preventing you from returning an existing object); like most design patterns (especially the plethora of them invented specifically for Java in the early days, due to language inflexibility), it's a way of working around language limitations in a consistent way. Python doesn't have that limitation; you use__new__ for this case, and@classmethod alternate constructors for construction from variant arguments.
Link for Factory pattern Recipe does not work.
I arrived here just after reading a RealPython blog about it. I liked it a lot:realpython.com/factory-method-python
210

__new__ is static class method, while__init__ is instance method.__new__ has to create the instance first, so__init__ can initialize it. Note that__init__ takesself as parameter. Until you create instance there is noself.

Now, I gather, that you're trying to implementsingleton pattern in Python. There are a few ways to do that.

Also, as of Python 2.6, you can use classdecorators.

def singleton(cls):    instances = {}    def getinstance():        if cls not in instances:            instances[cls] = cls()        return instances[cls]    return getinstance@singletonclass MyClass:  ...
Lorin Hochstein's user avatar
Lorin Hochstein
59.6k31 gold badges110 silver badges144 bronze badges
answeredMar 23, 2009 at 17:20
vartec's user avatar

13 Comments

@Tyler Long, I don't quite understand how does the "@singleton" work? Because the decorator returns a function but MyClass is a class.
Why the dictionary? Since cls will always be the same and you get a fresh dictionary for instance for each singleton you are creating a dictionary with only one item in it.
@Alcott: No opinion needed --the docs agree with you.
@Alcott. yes the decorator returns a function. but both the class and the function are a callable. I think instances = {} should be a global variable.
@TylerLong Alcott has a good point. This results in the nameMyClass being bound to a function that returns instances of the original class. But there's now no way to refer to the original class at all, andMyClass being a function breaksisinstance/issubclass checks, access to class attributes/methods directly asMyClass.something, naming the class insuper calls, etc, etc. Whether that's a problem or not depends on the class you're applying it to (and the rest of the program).
|
185

In most well-known OO languages, an expression likeSomeClass(arg1, arg2) will allocate a new instance, initialise the instance's attributes, and then return it.

In most well-known OO languages, the "initialise the instance's attributes" part can be customised for each class by defining aconstructor, which is basically just a block of code that operates on the new instance (using the arguments provided to the constructor expression) to set up whatever initial conditions are desired. In Python, this corresponds to the class'__init__ method.

Python's__new__ is nothing more and nothing less than similar per-class customisation of the "allocate a new instance" part. This of course allows you to do unusual things such as returning an existing instance rather than allocating a new one. So in Python, we shouldn't really think of this part as necessarily involving allocation; all that we require is that__new__ comes up with a suitable instance from somewhere.

But it's still only half of the job, and there's no way for the Python system to know that sometimes you want to run the other half of the job (__init__) afterwards and sometimes you don't. If you want that behavior, you have to say so explicitly.

Often, you can refactor so you only need__new__, or so you don't need__new__, or so that__init__ behaves differently on an already-initialised object. But if you really want to, Python does actually allow you to redefine "the job", so thatSomeClass(arg1, arg2) doesn't necessarily call__new__ followed by__init__. To do this, you need to create a metaclass, and define its__call__ method.

A metaclass is just the class of a class. And a class'__call__ method controls what happens when you call instances of the class. So ametaclass'__call__ method controls what happens when you call a class; i.e. it allows you toredefine the instance-creation mechanism from start to finish. This is the level at which you can most elegantly implement a completely non-standard instance creation process such as the singleton pattern. In fact, with less than 10 lines of code you can implement aSingleton metaclass that then doesn't even require you to futz with__new__at all, and can turnany otherwise-normal class into a singleton by simply adding__metaclass__ = Singleton!

class Singleton(type):    def __init__(self, *args, **kwargs):        super(Singleton, self).__init__(*args, **kwargs)        self.__instance = None    def __call__(self, *args, **kwargs):        if self.__instance is None:            self.__instance = super(Singleton, self).__call__(*args, **kwargs)        return self.__instance

However this is probably deeper magic than is really warranted for this situation!

answeredDec 29, 2011 at 7:39
Ben's user avatar

3 Comments

Amazing :o simple and doesnt make working within your class annoying like decorating it would do.
self should becls in metaclass Singleton
@AmauryLeRouxDupeyron That's a stylistic choice, not a requirement (as is the nameself in the first place, TBH). In this case I preferred to emphasise that the behaviour we're using here is perfectly ordinary class stuff, because metaclasses are perfectly ordinary classes. If the metaclass was more complicated and there needed to be code here that was run at the class level and thus had the object-levelself in scope, then I'd usecls andself to keep the levels straight. But here there's only one level (directly) involved.
33

To quote thedocumentation:

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.

...

If __new__() does not return an instance of cls, then the new instance's __init__() method will not be invoked.

__new__() is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation.

rob's user avatar
rob
37.9k2 gold badges62 silver badges67 bronze badges
answeredMar 23, 2009 at 17:29
tgray's user avatar

3 Comments

If __new__() does not return an instance of cls, then the new instance's __init__() method will not be invoked. That's an important point. If you return a different instance, original__init__ is never called.
@tgray I realize your answer is from the docs, but I'm curious if you know any use-case of not returning an instance of cls. It seems like when the returned object of__new__ is checked for its class, the method would throw an error rather than allow the failed check to pass silently, because I do not understand why you'd ever want to return anything but an object of the class.
@soporific312 I haven't seen any use-cases.This answer discusses some of the reasoning for the design, though they have also not seen any code that leverages that "feature".
15

I realize that this question is quite old but I had a similar issue.The following did what I wanted:

class Agent(object):    _agents = dict()    def __new__(cls, *p):        number = p[0]        if not number in cls._agents:            cls._agents[number] = object.__new__(cls)        return cls._agents[number]    def __init__(self, number):        self.number = number    def __eq__(self, rhs):        return self.number == rhs.numberAgent("a") is Agent("a") == True

I used this page as a resourcehttp://infohost.nmt.edu/tcc/help/pubs/python/web/new-new-method.html

Aleksei Zyrianov's user avatar
Aleksei Zyrianov
2,3821 gold badge25 silver badges33 bronze badges
answeredMay 13, 2013 at 14:12

1 Comment

Note: __new__ always returns an appropriate object, so __init__ is always called - even if the instance already exists.
13

When__new__ returns instance of the same class,__init__ is run afterwards on returned object. I.e. you can NOT use__new__ to prevent__init__ from being run. Even if you return previously created object from__new__, it will be double (triple, etc...) initialized by__init__ again and again.

Here is the generic approach to Singleton pattern which extends vartec answer above and fixes it:

def SingletonClass(cls):    class Single(cls):        __doc__ = cls.__doc__        _initialized = False        _instance = None        def __new__(cls, *args, **kwargs):            if not cls._instance:                cls._instance = super(Single, cls).__new__(cls, *args, **kwargs)            return cls._instance        def __init__(self, *args, **kwargs):            if self._initialized:                return            super(Single, self).__init__(*args, **kwargs)            self.__class__._initialized = True  # Its crucial to set this variable on the class!    return Single

Full story ishere.

Another approach, which in fact involves__new__ is to use classmethods:

class Singleton(object):    __initialized = False    def __new__(cls, *args, **kwargs):        if not cls.__initialized:            cls.__init__(*args, **kwargs)            cls.__initialized = True        return clsclass MyClass(Singleton):    @classmethod    def __init__(cls, x, y):        print "init is here"    @classmethod    def do(cls):        print "doing stuff"

Please pay attention, that with this approach you need to decorate ALL of your methods with@classmethod, because you'll never use any real instance ofMyClass.

answeredJan 26, 2015 at 21:25
Zaar Hai's user avatar

Comments

10

I think the simple answer to this question is that, if__new__ returns a value that is the same type as the class, the__init__ function executes, otherwise it won't. In this case your code returnsA._dict('key') which is the same class ascls, so__init__ will be executed.

answeredJul 7, 2013 at 14:45
PMN's user avatar

Comments

8

An update to @AntonyHatchkins answer, you probably want a separate dictionary of instances for each class of the metatype, meaning that you should have an__init__ method in the metaclass to initialize your class object with that dictionary instead of making it global across all the classes.

class MetaQuasiSingleton(type):    def __init__(cls, name, bases, attibutes):        cls._dict = {}    def __call__(cls, key):        if key in cls._dict:            print('EXISTS')            instance = cls._dict[key]        else:            print('NEW')            instance = super().__call__(key)            cls._dict[key] = instance        return instanceclass A(metaclass=MetaQuasiSingleton):    def __init__(self, key):        print 'INIT'        self.key = key        print()

I have gone ahead and updated the original code with an__init__ method and changed the syntax to Python 3 notation (no-arg call tosuper and metaclass in the class arguments instead of as an attribute).

Either way, the important point here is that your class initializer (__call__ method) will not execute either__new__ or__init__ if the key is found. This is much cleaner than using__new__, which requires you to mark the object if you want to skip the default__init__ step.

answeredJul 18, 2017 at 19:39
Mad Physicist's user avatar

7 Comments

And how do you provide a parameter to the metaclass so that all instances of the classA have the value in their class that you gave to the metaclass? How to get a variable argument into the metaclass__init__?
@NeilG. You can pass kwargs when you call the metaclass (e.g. with aclass statement)
Ah!There!, @Mad_Physicist! Thanks! You mean likeclass A(metaclass=MetaQuasiSingleton(name="value")): ... ?
@NeilG. Probably more likeclass A(metaclass=MetaQuasiSingleton, name="value"): ... . Your proposed syntax callsMetaQuasiSingleton, which might return a class object as the metaclass or crash.
@NeilG. Fair enough. Good luck. I'm sure you'll ask a good question when you need to.
|
7
class M(type):    _dict = {}    def __call__(cls, key):        if key in cls._dict:            print 'EXISTS'            return cls._dict[key]        else:            print 'NEW'            instance = super(M, cls).__call__(key)            cls._dict[key] = instance            return instanceclass A(object):    __metaclass__ = M    def __init__(self, key):        print 'INIT'        self.key = key        printa1 = A('aaa')a2 = A('bbb')a3 = A('aaa')

outputs:

NEWINITNEWINITEXISTS

NB As a side effectM._dict property automatically becomes accessible fromA asA._dict so take care not to overwrite it incidentally.

answeredJul 30, 2015 at 12:04
Antony Hatchkins's user avatar

1 Comment

You are missing an__init__ method that setscls._dict = {}. You probably don't want a dictionary shared by all classes of this metatype (but +1 for the idea).
7

However, I'm a bit confused as to why__init__ is always called after__new__.

I think the C++ analogy would be useful here:

  1. __new__ simply allocates memory for the object. The instance variables of an object needs memory to hold it, and this is what the step__new__ would do.

  2. __init__ initialize the internal variables of the object to specific values (could be default).

Bryan's user avatar
Bryan
2,85525 gold badges39 silver badges45 bronze badges
answeredFeb 11, 2019 at 22:38
HAltos's user avatar

1 Comment

"new simply allocates memory for the object". This is not correct, first you can do whatever your want innew, including adding and initializing attributes of the newly created instance, second you can create nothing innew if that suits your need, and therefore allocate no memory at all. Thesingleton example on this page uses both possibilities at different times: On the first call it creates/initializes an attribute, on other calls it just returns an existing instance (it must be innew asinit returns nothing).
6

__new__ should return a new, blank instance of a class. __init__ is then called to initialise that instance. You're not calling __init__ in the "NEW" case of __new__, so it's being called for you. The code that is calling__new__ doesn't keep track of whether __init__ has been called on a particular instance or not nor should it, because you're doing something very unusual here.

You could add an attribute to the object in the __init__ function to indicate that it's been initialised. Check for the existence of that attribute as the first thing in __init__ and don't proceed any further if it has been.

SilentGhost's user avatar
SilentGhost
322k67 gold badges312 silver badges294 bronze badges
answeredMar 23, 2009 at 17:23
Andrew Wilkinson's user avatar

Comments

5

Digging little deeper into that!

The type of a generic class in CPython istype and its base class isObject (Unless you explicitly define another base class like a metaclass). The sequence of low level calls can be foundhere. The first method called is thetype_call which then callstp_new and thentp_init.

The interesting part here is thattp_new will call theObject's (base class) new methodobject_new which does atp_alloc (PyType_GenericAlloc) which allocates the memory for the object :)

At that point the object is created in memory and then the__init__ method gets called. If__init__ is not implemented in your class then theobject_init gets called and it does nothing :)

Thentype_call just returns the object which binds to your variable.

answeredNov 15, 2016 at 20:47
xpapazaf's user avatar

Comments

5

One should look at__init__ as a simple constructor in traditional OO languages. For example, if you are familiar with Java or C++, the constructor is passed a pointer to its own instance implicitly. In the case of Java, it is thethis variable. If one were to inspect the byte code generated for Java, one would notice two calls. The first call is to an "new" method, and then next call is to the init method (which is the actual call to the user defined constructor). This two step process enables creation of the actual instance before calling the constructor method of the class which is just another method of that instance.

Now, in the case of Python,__new__ is a added facility that is accessible to the user. Java does not provide that flexibility, due to its typed nature. If a language provided that facility, then the implementor of__new__ could do many things in that method before returning the instance, including creating a totally new instance of a unrelated object in some cases. And, this approach also works out well for especially for immutable types in the case of Python.

Alastair Irvine's user avatar
Alastair Irvine
1,17812 silver badges16 bronze badges
answeredApr 24, 2014 at 1:30
grdvnl's user avatar

Comments

5

Referring to this doc:

When subclassing immutable built-in types like numbers and strings,and occasionally in other situations, the static method__new__ comesin handy.__new__ is the first step in instance construction, invokedbefore__init__.

The__new__ method is called with the class as itsfirst argument; its responsibility is to return a new instance of thatclass.

Compare this to__init__:__init__ is called with an instanceas its first argument, and it doesn't return anything; itsresponsibility is to initialize the instance.

There are situationswhere a new instance is created without calling__init__ (for examplewhen the instance is loaded from a pickle). There is no way to createa new instance without calling__new__ (although in some cases you canget away with calling a base class's__new__).

Regarding what you wish to achieve, there also in same doc info about Singleton pattern

class Singleton(object):        def __new__(cls, *args, **kwds):            it = cls.__dict__.get("__it__")            if it is not None:                return it            cls.__it__ = it = object.__new__(cls)            it.init(*args, **kwds)            return it        def init(self, *args, **kwds):            pass

you may also use this implementation from PEP 318, using a decorator

def singleton(cls):    instances = {}    def getinstance():        if cls not in instances:            instances[cls] = cls()        return instances[cls]    return getinstance@singletonclass MyClass:...
Cristian Ciupitu's user avatar
Cristian Ciupitu
21.1k7 gold badges56 silver badges80 bronze badges
answeredSep 30, 2013 at 6:13
kiriloff's user avatar

1 Comment

Calling a bastardized form ofinit from__new__ sounds really hacky. This is what metaclasses are for.
3

Now I've got the same problem, and for some reasons I decided to avoid decorators, factories and metaclasses. I did it like this:

Main file

def _alt(func):    import functools    @functools.wraps(func)    def init(self, *p, **k):        if hasattr(self, "parent_initialized"):            return        else:            self.parent_initialized = True            func(self, *p, **k)    return initclass Parent:    # Empty dictionary, shouldn't ever be filled with anything else    parent_cache = {}    def __new__(cls, n, *args, **kwargs):        # Checks if object with this ID (n) has been created        if n in cls.parent_cache:            # It was, return it            return cls.parent_cache[n]        else:            # Check if it was modified by this function            if not hasattr(cls, "parent_modified"):                # Add the attribute                cls.parent_modified = True                cls.parent_cache = {}                # Apply it                cls.__init__ = _alt(cls.__init__)            # Get the instance            obj = super().__new__(cls)            # Push it to cache            cls.parent_cache[n] = obj            # Return it            return obj

Example classes

class A(Parent):    def __init__(self, n):        print("A.__init__", n)class B(Parent):    def __init__(self, n):        print("B.__init__", n)

In use

>>> A(1)A.__init__ 1  # First A(1) initialized <__main__.A object at 0x000001A73A4A2E48>>>> A(1)      # Returned previous A(1)<__main__.A object at 0x000001A73A4A2E48>>>> A(2)A.__init__ 2  # First A(2) initialized<__main__.A object at 0x000001A7395D9C88>>>> B(2)B.__init__ 2  # B class doesn't collide with A, thanks to separate cache<__main__.B object at 0x000001A73951B080>
  • Warning: You shouldn't initialize Parent, itwill collide with other classes - unless you defined separate cache in each of the children, that's not what we want.
  • Warning: It seems a class with Parent as grandparent behaves weird. [Unverified]

Try it online!

answeredJul 25, 2018 at 19:21

Comments

2

The__init__ is called after__new__ so that when you override it in a subclass, your added code will still get called.

If you are trying to subclass a class that already has a__new__, someone unaware of this might start by adapting the__init__ and forwarding the call down to the subclass__init__. This convention of calling__init__ after__new__ helps that work as expected.

The__init__ still needs to allow for any parameters the superclass__new__ needed, but failing to do so will usually create a clear runtime error. And the__new__ should probably explicitly allow for*args and '**kw', to make it clear that extension is OK.

It is generally bad form to have both__new__ and__init__ in the same class at the same level of inheritance, because of the behavior the original poster described.

answeredJun 18, 2012 at 16:30
Jay Obermark's user avatar

Comments

1

However, I'm a bit confused as to why__init__ is always called after__new__.

Not much of a reason other than that it just is done that way.__new__ doesn't have the responsibility of initializing the class, some other method does (__call__, possibly-- I don't know for sure).

I wasn't expecting this. Can anyone tell me why this is happening and how I implement this functionality otherwise? (apart from putting the implementation into the__new__ which feels quite hacky).

You could have__init__ do nothing if it's already been initialized, or you could write a new metaclass with a new__call__ that only calls__init__ on new instances, and otherwise just returns__new__(...).

answeredMar 23, 2009 at 17:23
Devin Jeanpierre's user avatar

Comments

1

The simple reason is that thenew is used for creating an instance, whileinit is used for initializing the instance. Before initializing, the instance should be created first. That's whynew should be called beforeinit.

answeredMar 23, 2018 at 13:48
ZQ Hu's user avatar

Comments

1

When instantiating a class, first,__new__() is called to createthe instance of a class, then__init__() is called to initializethe instance.

__new__():

Called to create a new instance of class cls. ...

If __new__() is invoked during object construction and it returns aninstance of cls, then the new instance’s __init__() method will beinvoked like __init__(self[, ...]), ...

__init__():

Called after the instance has been created (by __new__()), ...

Because __new__() and __init__() work together in constructing objects(__new__() to create it, and __init__() to customize it), ...

For example, when instantiatingTeacher class, first,__new__() is called to createthe instance ofTeacher class, then__init__() is called to initializethe instance as shown below:

class Teacher:    def __init__(self, name):        self.name = name        class Student:    def __init__(self, name):        self.name = nameobj = Teacher("John") # Instantiationprint(obj.name)

This is the output:

<class '__main__.Teacher'>John

And, using__new__() ofthe instance ofTeacher class, we can createthe instance ofStudent class as shown below:

# ...obj = Teacher("John")print(type(obj))print(obj.name)obj = obj.__new__(Student) # Creates the instance of "Student" classprint(type(obj))

Now,the instance ofStudent class is created as shown below:

<class '__main__.Teacher'><__main__.Teacher object at 0x7f4e3950bf10><class '__main__.Student'> # Here

Next, if we try to getthe value ofname variable from **the instance ofStudent class as shown below:

obj = Teacher("John")print(type(obj))print(obj.name)obj = obj.__new__(Student)print(type(obj))print(obj.name) # Tries to get the value of "name" variable

The error below occurs becausethe instance ofStudent class has not been initialized by__init__() yet:

AttributeError: 'Student' object has no attribute 'name'

So, we initializethe instance ofStudent class as shown below:

obj = Teacher("John") print(type(obj))print(obj.name)obj = obj.__new__(Student)print(type(obj))obj.__init__("Tom") # Initializes the instance of "Student" classprint(obj.name)

Then, we can getthe value ofname variable fromthe instance ofStudent class as shown below:

<class '__main__.Teacher'>John<class '__main__.Student'>Tom # Here
answeredNov 20, 2022 at 16:38
Super Kai - Kazuya Ito's user avatar

Comments

1

People have already detailed the question and answer both use some examples like singleton etc. See the code below:

__instance = Nonedef __new__(cls):    if cls.__instance is None:        cls.__instance = object.__new__(cls)    return cls.__instance

Execution of above code

I got the above code from thislink, it has detailed overview ofnew vsinit. Worth reading!

answeredFeb 16, 2023 at 13:30
Unkownn's user avatar

Comments

Protected question. To answer this question, you need to have at least 10 reputation on this site (not counting theassociation bonus). The reputation requirement helps protect this question from spam and non-answer activity.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.