If you have a class Foo then:

  • Foo() is the constructor
  • Foo.__init__() is the initializer
  • Foo.__new__() is the allocator

Construction of a Python object is simply allocation of a new instance followed by initialization of said instance.

Answer from Ignacio Vazquez-Abrams on Stack Overflow
Top answer
1 of 6
59

If you have a class Foo then:

  • Foo() is the constructor
  • Foo.__init__() is the initializer
  • Foo.__new__() is the allocator

Construction of a Python object is simply allocation of a new instance followed by initialization of said instance.

2 of 6
57

Personally, I find "__init__ is not a constructor" to be pretty fine hair-splitting.

__init__ is called when a new object is requested. It is supposed to use its arguments to assign attributes on the new object, such that the required invariants for normal operation of the object are set up. The object is already a valid pre-existing place to store attributes by the time the code in __init__ begins running. The new object normally has no attributes defined on it already when the code in __init__ begins running (other than the ones that all objects possess).

A C++ constructor is called when a new object is requested. It is supposed to use its arguments to assign to fields on the new object, such that the required invariants for normal operation of the object are set up. The object is already a valid pre-existing place to store fields by the time the code in the constructor begins running. The new object has all its declared fields already when the code in the constructor begins running, but they contain garbage.

A Java constructor is called when a new object is requested. It is supposed to use its arguments to assign to fields on the new object, such that the required invariants for normal operation of the object are set up. The object is already a valid pre-existing place to store fields by the time the code in the constructor begins running. The new object has all its declared fields already when the code in the constructor begins running, with their default values.

The major difference between an __init__ method and a C++/Java constructor is in that last sentence I've highlighted, and that's just the difference between the static nature of Java/C++ and the dynamic nature of Python. I don't think this warrants calling them fundamentally different concepts that must not be referred to by the same word.

I think the main reason Pythonistas don't like to refer to __init__ as a constructor is that people think of C++/Java constructors as "making a new object", because that's what they seem to do when you call them. But there's really two things going on when you call a constructor; a new object is created and then the constructor is called to initialise it. In C++/Java the "create a new object" part of that is invisible, whereas that can be exposed/customised in Python (via the __new__ method).

So while the role of the __init__ method is extremely similar to the role of a C++/Java constructor, some people prefer to emphasise the fact that this isn't the whole process by saying that "__init__ is not a constructor".

Discussions

Is a constructor __init__ necessary for a class in Python? - Stack Overflow
I read that the constructor is like the first argument passed to the class, which makes sense to me since the parameters seem to be passed to the class via the __init__ method. For example, class More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Initializer vs Constructor - Stack Overflow
I have heard that the __init__ function in python is not a Constructor, It's an Initializer and actually the __new__ function is the Constructor and the difference is that the __init__ function is ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
What is the difference between __init__ and __new__?

Ok, this is a FAQ but a good one. When you know this one, you know how python instantiates objects at the python level.

So here is the thing. When __init__ executes, you get a first parameter that is the instance of your class. Normally, this first parameter is called self. Inside init you do all you want on this empty instance, normally set member vars.

However, that instance has been created somehow. Who creates it?

here is where __new__ enters the game. __new__ is a class method, that is, when executed, it gets passed the class. The objective of new is to create the instance that will then emerge as self into __init__.

What is the default implementation of __new__? Generally, for a simple class (e.g. has no parents) it just calls object.__new__, something that creates a new instance of your class, but you can override it and do something before or after that. Technically, you could only use __new__, put all the stuff you have in __init__ just after the call to object.__new__() and be done with it. In practice, you prefer init because you don't want to repeat all the boilerplate to create the instance, which is mostly the same for all classes, and focus only on the unique part, that is, the initialization.

When should you use __new__? There are some special cases where you want to, but in general, ask yourself the question: do I need to introduce this logic before the instance is created? if yes, then you need to override __new__. If the answer is no, then you should put it in __init__

More on reddit.com
๐ŸŒ r/learnpython
17
11
January 11, 2015
is it possible to ignore some fields when creating a Python dataclass from a .csv?
Is there a way to have it ignore extra fields? Sure, but you have to actually do it. Out of curiosity, did you find this bit of code, or did you create it yourself? The reason I ask is that using **row is a slightly more advanced Python expression and it would make sense why you're having this problem if you grabbed that part without understanding it. So first, let's break down what it's doing. The list comprehension is functionally similar to doing an operation on a loop. So what happens if we take this code and just look at row? people = [row for row in reader] You will probably see something like this if you run this code with just the three columns: [{'name': 'John Doe', 'age': '25', 'city': 'Houston'}, {'name': 'Beth Doe', 'age': '22', 'city': 'San Francisco'}] This is actually the same data the DictReader gives us. So what does the asterisk do? This is somewhat complicated, but it essentially breaks it into a tuple (single asterisk) or dictionary (double asterisk). For example, check this code based on the reader: for person in reader: print(*person) # Output name age city name age city What's happening here is that each key value is being printed out. Trying **person won't work, because it's trying to push a dictionary into print, but basically it creates arguments with key/value pairs, similar to using var=None in a parameter list for a function, where this would be `{"var": None} in dictionary format. Now that we know that, let's look back at your original code: people = [Person(**row) for row in reader] That Person(**row) is the cause of your error: DictReader is going to read every heading, so if you have 4 headings, such as state being including, it's equivalent to doing something like this: Person(name="name", age="age", city="city", state="state") The problem, of course, is that the Person dataclass doesn't have a state property, so this is undefined behavior. How can you fix this, then? Assuming you only want those three elements to represent a person, you'll need to skip the list comprehension method and do your loop manually, ignoring the fields you don't need. For example, something like this: people = [] for person in reader: new_person = Person( name = person["name"], age = int(person["age"]), city = person["city"], ) people.append(new_person) There are other ways to do this, of course, but this is the simplest. Essentially, you loop through each row, and create a new Person object with just the data from that row you want, and then you append that to a list. This will give you the same core data as your previous list comprehension but will ignore anything that isn't a row you want. More on reddit.com
๐ŸŒ r/learnpython
12
8
September 4, 2024
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ __init__-in-python
__init__ in Python - GeeksforGeeks
__init__ method in Python is a constructor. It runs automatically when a new object of a class is created. Its main purpose is to initialize the objectโ€™s attributes and set up its initial state.
Published ย  September 12, 2025
๐ŸŒ
Towards Data Science
towardsdatascience.com โ€บ home โ€บ latest โ€บ python: __init__ is not a constructor: a deep dive in python object creation
Python: __init__ is NOT a constructor: a deep dive in Python object creation | Towards Data Science
January 19, 2025 - Constructors actually create the new object; the __init__ method is only responsible for setting the state of the object. It just receives values through itโ€™s parameters and assigns them to the class attributes like greet_name.
๐ŸŒ
Great Learning
mygreatlearning.com โ€บ blog โ€บ it/software development โ€บ python __init__:ย an overview
Python __init__: An Overview
October 14, 2024 - Once the __init__ of Person finishes ... finishes its execution. So, to sum it all up, __init__ is a reserved method for classes in Python that basically behaves as the constructors....
๐ŸŒ
Quora
quora.com โ€บ Can-we-say-init__-self-is-a-constructor-in-Python-How-can-this-be-justified
Can we say' __init__ (self) ' is a constructor in Python? How can this be justified? - Quora
Answer (1 of 4): Although we use __init__ almost in same sense as as a C++/Java constructor while programming (to create members of a new class instance) , it is not really the same in how it works because Python is a dynamic language while C++/Java are static. Now one may/may not choose to call ...
๐ŸŒ
StrataScratch
stratascratch.com โ€บ blog โ€บ what-is-the-purpose-of-__init__-in-python
What Is the Purpose of __init__ in Python? - StrataScratch
November 7, 2024 - It allows you to define custom ... creation. This method is commonly mistaken for a constructor, but in Python, the actual constructor is the __new__ method, which is responsible for creating an instance....
Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ analytics-vidhya โ€บ is-init-the-constructor-in-python-43bb744e73b
Is __init__() the constructor in python? | by Sambhav Choradia | Analytics Vidhya | Medium
March 11, 2020 - It is not a keyword and has no special meaning in Python. We could use other names (like this). The constructor is a method which creates the object. ... __init__() is not a constructor.
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 1103113 โ€บ is-python-init-a-constructor
Is python init a constructor? | Sololearn: Learn to code for FREE!
__new__ is the constructor, but it is not usually used in classical programming. __init__, however, is the initialiser, it populates the instance with the given arguments and then you have a working instance
๐ŸŒ
Python Pool
pythonpool.com โ€บ home โ€บ blog โ€บ init python | is __init__ in python a constructor?
init Python | Is __init__ in Python a Constructor? - Python Pool
January 1, 2024 - In python init can be used as a construtor. A constructor is called as soon as an object of the same class is initialised.
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ python โ€บ init-function
Python __init__() Function: Syntax, Usage, and Examples
The Python __init__() function serves as the constructor method for classes. It initializes newly created objects and allows you to assign values to object properties or run startup procedures when an instance is created.
๐ŸŒ
Real Python
realpython.com โ€บ python-class-constructor
Python Class Constructors: Control Your Object Instantiation โ€“ Real Python
January 19, 2025 - Python handles instantiation internally with .__new__() for creation and .__init__() for initialization. You can customize object initialization by overriding the .__init__() method in your class.
๐ŸŒ
APXML
apxml.com โ€บ courses โ€บ python-for-beginners โ€บ chapter-8-introduction-object-oriented-concepts โ€บ python-init-method
Python __init__ Method | Class Constructor
Python provides a special method for this purpose: __init__. This method is automatically called whenever you create a new instance (object) of a class. Because it initializes the object's state, it's often referred to as the class constructor, although technically it initializes an already ...
๐ŸŒ
DEV Community
dev.to โ€บ pila โ€บ constructors-in-python-init-vs-new-2f9j
Constructors in Python (__init vs __new__) - DEV Community
September 16, 2020 - It's called first and is responsible for returning a new instance of your class. In contrast, __init__ doesn't return anything; it's only responsible for initializing the instance after it's been created. In general, you shouldn't need to override __new__unless you're subclassing an immutable type like str, int, Unicode, or tuple. NOTE: Never name a function of your own with leading and trailing double underscores. It may mean nothing to Python, but there's always the possibility that the designers of Python will add a function that has a special purpose with that name in the future, and when they do, your code will break.
๐ŸŒ
Quora
quora.com โ€บ What-is-the-use-of-the-init__-function-in-Python
What is the use of the __init__ function in Python? - Quora
Constructor is used to initialize class variables. So, in python __init__ is nothing but a constructor ๐Ÿ˜….
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ How-does-constructor-method-init-work-in-Python
How does constructor method __init__ work in Python?
July 16, 2020 - The constructor's role is to assign value to instance variables as soon as the object is declared. Python uses a special method called __init__() to initialize the instance variables for the object, as soon as it is declared.
๐ŸŒ
CodeSignal
codesignal.com โ€บ learn โ€บ courses โ€บ clean-coding-with-classes-2 โ€บ lessons โ€บ constructors-and-object-initialization-in-python
Constructors and Object Initialization in Python
How Constructors and Object Initialization are Important to Clean Code ยท The __init__ method in Python is fundamental for initializing objects in a known state, thereby enhancing code maintainability and readability. This method encapsulates the logic of object creation, ensuring that each ...
Top answer
1 of 6
7

I see a misconception here between a constructor--constructing the object and initializing the object:

Python's use of __new__ and __init__?

Use __new__ when you need to control the creation of a new instance. Use __init__ when you need to control initialization of a new instance.

So we must be careful here.

I read that the constructor is like the first argument passed to the class, which makes sense to me since the parameters seem to be passed to the class via the __init__ method.

The constructor is not passed to the class, to be precise the result of the constructor (__new__) will be the first argument for every instance method in the class or its sub-classes (note: __new__ works only for new-style classes):

class A:
    def __new__(self):
            return 'xyz'

See what happens when you call the class (create the object):

>>> A()
'xyz'
>>> type(A())
<class 'str'>

Calling the class no longer return instance of type A, because we changed the mechanism of the constructor __new__. Actually by doing so you alter the whole meaning of your class, not only, this is pretty much hard to decipher. It's unlikely that you'll switch the type of object during the creating time of that specific object. I hope this sentence makes sense, if not, how will it make sense in your code!

class A:
    def __new__(self):
            return 'xyz'

    def type_check(self):
            print(type(self))

Look what happens when we try to call type_check method:

>>> a = A()
>>> a
'xyz'
>>> a.type_check()
AttributeError: 'str' object has no attribute 'type_check'

a is not an object of class A, so basically you don't have access to class A anymore.

__init__ is used to initialize the object's state. Instead of calling methods that will initialize the object's members after it's created, __init__ solves this issue by initializing the object's members during creation time, so if you have a member called name inside a class and you want to initialize name when you create the class instead of calling an extra method init_name('name'), you would certainly use __init__ for this purpose.

So when I 'call' the class, I pass it the parameters from the __init__ method?

When you call the class, you pass the parameters (to) __init__ method?

Whatever arguments you pass the class, all the parameters will be passed to __init__ with one additional parameter added automatically for you which is the implied object usually called self (the instance itself) that will be passed always as the left-most argument by Python automatically:

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

        A(  34,  35) 
 self.a = 34 |    |  
             |    | 
             |    | self.b = 35  
  init(self, a,   b)
        |
        |
        | 
       The instance that you created by calling the class A() 

Note: __init__ works for both classic classes and new style classes. Whereas, __new__ works only for new-style classes.

2 of 6
4

No, the constructor is just the method that is called to construct the object. It is not passed anywhere. Rather the object itself is passed automatically to all methods of the class.

Constructor is not required if you don't have anything to construct but usually you have something to do in the beginning.

๐ŸŒ
DEV Community
dev.to โ€บ delta456 โ€บ python-init-is-not-a-constructor-12on
Python: __init__() is not the only constructor - DEV Community
February 8, 2020 - Exactly. Coming from C++ I'd say the function that initializes new objects is the constructor (this term is also used by the ISO specification). What this article is saying is that the Python equivalence of allocator in C++, or the implicit ...
Top answer
1 of 3
27

In essence, __new__ is responsible for creating the instance (thus, it may be accurate to say that it is the constructor, as you've noted) while __init__ is indeed a way of initializing state in an instance. For example, consider this:

class A(object):

    def __new__(cls):
        return object.__new__(cls)

    def __init__(self):
        self.instance_method()

    def instance_method(self):
        print 'success!'

newA = A()

Notice that __init__ receives the argument self, while __new__ receives the class (cls). Since self is a reference to the instance, this should tell you quite evidently that the instance is already created by the time __init__ gets called, since it gets passed the instance. It's also possible to call instance methods precisely because the instance has already been created.

As to your second question, there is rarely a need in my experience to use __new__. To be sure, there are situations where more advanced techniques might make use of __new__, but those are rare. One notorious example where people might be tempted to use __new__ is in the creation of the Singleton class (whether that's a good technique or not, however, isn't the point).

For better or worse, you basically get to control the process of instantiation, and whatever that might mean in your specific situation.

2 of 3
5

__init__ is called with an already built up instance of the object as first parameter (normally called self, but that's just a parameter name).

__new__ instead is called passing the class as first parameter and is expected to return an instance (that will be later passed to __init__).

This allows for example __new__ to return an already-existent instance for value-based objects that are immutable and for which identity shouldn't play a role.