- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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.
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()
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.
- Why Python is called Dynamically Typed?
- Why is petroleum called black gold?
- Why is a smartphone called smart?
- Why do people always advise you to cry after a tragedy?
- Why singleton class is always sealed in C#?
- Why is CNG called as clean fuel?
- Why the main () method in Java is always static?
- Which herb is called a magical herb and why?
- Why is Napoleon Bonaparte called a Great War strategist?
- Why is Greenland called as island and not a continent?
- Why are TV serials called daily soaps?
- Why is Greenland called as an island and not a continent?
- Why Naïve Bayesian is classifications called Naïve?
- Why are South Africans called ‘Protease’?