0

I am pretty new to Python. I just started to gather knowledge about OOP in python.

In OOP ,there is always the concept of constructor,but in python we see a different scenario. that is, it does not have a explicit constructor.

Though the __init__() method is considered a constructor in python, just not explicitly... It behaves in many ways like a constructor. e.g. it is the first lines of code which are executed. But strictly speaking, it would be wrong to call it a constructor. A new instance or the class in question, is already "constructed" by the time the method __init__ is called. Could anyone provide a good explanation with example on this line I wrote: "because a new instance is already "constructed" by the time the method __init__ is called"

Md. Tanvir Raihan
  • 4,075
  • 9
  • 37
  • 70
  • 2
    I don't understand your question. Your explanation is correct. Why do you want another? (However, `__init__` is not "the first code which is executed". `__new__` is executed first, and that is the constructor.) – BrenBarn Aug 16 '16 at 03:41
  • How is this different from constructors in C++ and PHP? They all run after the new object is constructed by the system, then they initialize elements. – Barmar Aug 16 '16 at 03:43
  • 1
    I think he's looking for something more in terms of implementation details. – JesseTG Aug 16 '16 at 03:45
  • 1
    It would probably be more accurate to call these "initializers", but "constructor" has become the traditional term. – Barmar Aug 16 '16 at 03:45
  • 1
    Possible duplicate of [Python's use of \_\_new\_\_ and \_\_init\_\_?](http://stackoverflow.com/questions/674304/pythons-use-of-new-and-init) – donkopotamus Aug 16 '16 at 03:57

0 Answers0