Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Using Python Class Constructors (Summary)

Now you know how Python class constructors allow you to instantiate classes, so you can create concrete and ready-to-use objects in your code. In Python, class constructors internally trigger the instantiation or construction process, which goes through instance creation and instance initialization. These steps are driven by the .__new__() and .__init__() special methods.

By learning about Python’s class constructors, the instantiation process, and the .__new__() and .__init__() methods, you can now manage how your custom classes construct new instances.

In this video course, you learned:

  • How Python’s instantiation process works internally
  • How your own .__init__() methods help you customize object initialization
  • How overriding the .__new__() method allows for custom object creation

Now you’re ready to take advantage of this knowledge to fine-tune your class constructors and take full control over instance creation and initialization in your object-oriented programming adventure with Python.

To learn more about the concepts in this course, you can check out:


Sample Code (.zip)

2.2 KB

Course Slides (.pdf)

4.9 MB

Younes Boukroun on March 18, 2023

The last 2 parts were hard to understand for me, I think giving some real life exemples could help.

Cenk T on March 25, 2023

totally confused me with tuples ..

Darren Jones RP Team on March 28, 2023

@Cenk T - can you give me a bit more information about the confusion you’re having, and hopefully I’ll be able to help out?

@Younes - Are you talking about the Single Instance and Emulating namedtuple sections?

Younes Boukroun on March 29, 2023

YEs exactly @Darren

Mark Prill on Aug. 15, 2023

@Darren Jones - Everything made sense to me up until the last Emulating namedtuple example. I’ve been stepping through this code trying to understand it. Not sure how to ask this, but what confuses me is…

The code calls a function named_tuple_factory(), which returns a class (NamedTuple).

How do the class methods access the type_name, fields, and num_fields variables, since they were defined outside of the class? I’m guessing that it has something to do with the fact that when I look at the class type, it isn’t <class '__main__.NamedTuple'>, but rather <class '__main__.named_tuple_factory.<locals>.NamedTuple'>. The namespace includes the function - so those variables stick around even after the return of the function???

If I create two different named tuples:

Point = named_tuple_factory('Point', 'x', 'y')

Square = named_tuple_factory('Square', 's')

Both classes show the same class - <class '__main__.named_tuple_factory.<locals>.NamedTuple'>. How does the code distinguish the two when creating instances of each? Does it have to do with the "__slots__ = ()" line???

What do I google to better understand this?

harrylesnar24 on Nov. 2, 2023

Hi @Darren Jones Until Singleton Chapter I understood but when you approach to Partially Emulating collections.namedtuple it difficult to understand what you are trying to be conveying us.

It is better if you kindly provide more depth example of the last chapter where and why it be useful and core concept of the last chapter you explained once again explain with more simpler terms mean it be helpful for us

Become a Member to join the conversation.