- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Why is __init__() always called after __new__() in python?
Python is having special type of methods called magic methods named with preceded and double underscores.
if we want to talk about magic method __new__ then obviously will also need to talk about __init__ method. The magic method __new__ will be called when instance is being created.where as __init__ method will be called to initialize instance when you are creating instance.
Example
class X(): _dict = dict() def __new__(self): if 'data' in X._dict: print ("new instance Exists") return X._dict['data'] else: print ("magic method New") return super(X, self).__new__(self) def __init__(self): print ("instantiation") X._dict['data'] = self print ("") a1 = X() a2 = X() a3 = X()
Output
magic method New instantiation new instance Exists instantiation new instance Exists instantiation
important to remember : The __init__ function is called a constructor, or initializer, and is automatically called when you create a new instance of a class.
- Related Articles
- What is __init__.py in Python?
- Why Python is called Dynamically Typed?
- Why do people always advise you to cry after a tragedy?
- How does constructor method __init__ work in Python?
- How to create instance Objects using __init__ in Python?
- Why singleton class is always sealed in C#?
- What is difference between self and __init__ methods in python Class?\n\n
- Why the main () method in Java is always static?
- Why is poison ivy is called so?
- Why is a smartphone called smart?
- Why is petroleum called black gold?
- Why is javascript called Richer Interface?
- Why JavaScript is called Richer Interface?
- Explain the variables inside and outside of a class __init__() function in Python.
- Why is CNG called as clean fuel?

Advertisements