Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a constructer and initializer in python? [duplicate]

Possible Duplicate:
Python (and Python C API): new versus init

I'm at college just now and the lecturer was using the terms constructors and initializers interchangeably. I'm pretty sure that this is wrong though.

I've tried googling the answer but not found the answer I'm looking for.

like image 213
Sheldon Avatar asked May 25 '11 20:05

Sheldon


People also ask

What is the difference between initializer and constructor?

Constructor is a special non-static member function of a class that is used to initialize objects of its class type. In the definition of a constructor of a class, member initializer list specifies the initializers for direct and virtual bases and non-static data members.

What does __ init __ mean in Python?

The __init__ method is the Python equivalent of the C++ constructor in an object-oriented approach. The __init__ function is called every time an object is created from a class. The __init__ method lets the class initialize the object's attributes and serves no other purpose. It is only used within classes.

Is __ new __ constructor?

And you might have guessed, __new__ is the constructor and __init__ is the initializer. Please note that __new__ is implicit. Meaning that if you don't actually need to modify the creation of an instance of the class, you don't need to have a new method.

What is an initializer in Python?

initializer method. A special method in Python (called __init__) that is invoked automatically to set a newly created object's attributes to their initial (factory-default) state. instance. An object whose type is of some class. Instance and object are used interchangeably.


2 Answers

In most OO languages, they are the same step, so he's not wrong for things like java, c++, etc. In python they are done in two steps: __new__ is the constructor; __init__ is the initializer.

Here is another answer that goes into more detail about the differences between them.

like image 178
Daenyth Avatar answered Sep 27 '22 15:09

Daenyth


In almost all usual cases, Python does not have constructors in the same sense used by other OO languages because manually managing memory is generally discouraged. Instead, what you should usually do is define an __init__ method on the class. This method is called to initialize the new instance object automatically, first thing after it is constructed. Thus, it is not really a constructor, and talking about it as a constructor might confuse some people.

Of course some people want to call it a constructor because it is used a little bit like a constructor - fundamentally you can call it whatever you want as long as everyone understands what you are actually referring to. But in general, to be explicit and make yourself understood, call it an init method or something other than a constructor. Fundamentally, different languages just come with somewhat different terminology and speaking very clearly will always require adjustment to your subject matter and audience.

In Python it is possible to manage instance creation and destruction at a finer granularity, though you won't want to unless you know what you're doing. This is done by defining __new__ and __del__ methods to hook object instantiation and del statements. Whether these qualify as constructors and destructors precisely is a little more debatable (Python docs call the del method a destructor, but tend to be vaguer on what constitutes a constructor, e.g. including many functions which return object instances). I'd still encourage you to use the specific terminology for the language at hand, and in comparative discussions to define your terms up front. As always, your choice of terms while speaking involves tradeoffs between the audience being able to easily follow you and the audience potentially being led into confusion: if you are talking about memory management probably be as specific as possible, but if you are talking loosely then just use some word your audience understands and be ready to clarify.

Your instructor is being unclear at worst, I'm not aware of any one canonical definition of these terms but they might cause confusion for people who have learned very specific definitions from other languages.

like image 27
Sasha Avatar answered Sep 27 '22 17:09

Sasha