avatarYang Zhou

Summary

The provided content discusses the use of constructors in Python classes, distinguishing between junior and senior Python developers based on their understanding of the __new__() and __init__() methods.

Abstract

The article on the undefined website delves into the concept of constructors within Python class structures, emphasizing the importance of understanding both the __new__() and __init__() methods to differentiate between junior and senior Python developers. It explains that while the __init__() method is commonly known for initializing an instance, the __new__() method is equally crucial as it is responsible for creating the instance itself. The article provides code examples to illustrate how these methods work together and also touches on advanced topics such as implementing the Singleton Pattern using __new__(). It concludes with practical tips for using constructors correctly, including the necessity for __init__() to return None and the proper use of super().__init__() in subclasses.

Opinions

  • The author suggests that knowledge of the __new__() method is a mark of a senior Python developer, as many beginners are unfamiliar with it.
  • The article implies that a deeper understanding of constructors can be a deciding factor in job interviews, setting apart more experienced developers.
  • It is conveyed that while Python provides a default implementation of __new__() from the object class, developers should be aware of when and how to customize it, such as in the case of the Singleton Pattern.
  • The author encourages following best practices when using constructors, such as ensuring __init__() returns None and properly initializing instances by calling the parent class's __init__() method when necessary.
  • The article promotes the author's publication "TechToFreedom" and recommends an AI service, ZAI.chat, as a cost-effective alternative to ChatGPT Plus (GPT-4).

Constructors in Python Classes

Photo by James Pond on Unsplash

What Is A Constructor?

In class-based object-oriented programming, a constructor (abbreviation: ctor) is a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables. (From Wikipedia)

In an actual interview, questions about constructor can easily distinguish between junior and senior Python developers.

Interview Question: Explain the constructor of Python classes?

Junior Python developers will explain the __init__() method since it’s usually the first method to be defined when writing a class. In most cases, the interviewer will continue to ask what the __new__() method does? Many junior developers don’t know how it works even what it is.

Senior Python developers will explain both of the __new__() and __init__() methods. Because technically speaking, both of them define the Python constructor:

  • The __new__() method creates a new instance.
  • The __init__() method initialises that instance.

Obviously, we should create an instance before initialising it. So the __init__() method is called after the __new__() method. Let’s look at an example:

The above results show how the s=Student("Yang","Zhou") works:

  • A new instance was created by __new__() method firstly.
  • Then parameters of this instance were initialised by __init__() method.

Dive Into The __new__( ) Method

The __new__() is a static method (special-cased so we need not declare it as such) of a class to create instances. The first parameter is always the cls representing the class itself. Remaining parameters are those needed for the constructor. The return value should be a instance of this class.

If we don’t want to customize the __new__() method, there is no need to state it explicitly in our classes. Because Python has already helped us define it in object, where all Python classes inherited from.

Probably because its frequency of use is less than __init__() method, many Python beginners don’t know how and when to use __new__() method. In fact, it’s very useful. For example, we can implement Singleton Pattern to restrict a class can only produce one instance:

As the above example shown, we can only produce one instance using Singleton_Student. The s1 and s2 are the same instance.

More Tips to Use The Constructor Correctly

  1. __init__() method must return None , otherwise a TypeError will be raised.
  2. If the parent class has defined __init__() method, we should using super().__init__() to explicitly call it in subclasses’ __init__() method to ensure correct initialization.
  3. If __new__() method does not return an instance of the class, then the __init__()method will not be invoked.

Thanks for reading. If you like it, please follow my publication TechToFreedom, where you can enjoy other Python tutorials and topics about programming, technology and investment.

Programming
Python
Object Oriented
Recommended from ReadMedium