Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

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:

Download

Sample Code (.zip)

2.2 KB
Download

Course Slides (.pdf)

4.9 MB
Avatar image for Younes Boukroun

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.

Avatar image for Cenk T

Cenk T on March 25, 2023

totally confused me with tuples ..

Avatar image for Darren Jones

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?

Avatar image for Younes Boukroun

Younes Boukroun on March 29, 2023

YEs exactly @Darren

Avatar image for Mark Prill

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?

Avatar image for harrylesnar24

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.