00:00 Instantiating Classes in Python. Python supports object-oriented programming with classes that are straightforward to create and use. Python classes offer powerful features that can help you write better software.
00:15 You can think of classes like blueprints for objects, and individual examples of them are known as instances. In the same way you can build several houses from a single blueprint, you can build several instances from a class.
A pretty common special method is
.__init__(). This method provides what’s known as the instance initializer in Python. The method’s job is to initialize instance attributes with appropriate values when you instantiate a given class. In the
Person class, the
.__init__() method’s first argument is called
This argument holds the current object or instance, which is passed implicitly in the method call. The argument is common to every instance method in Python. In this particular case, the second argument to
.__init__() is called
name and will hold the person’s name as a string.
Note that using
self to name the current object is a strong convention in Python, but not a requirement. However, using another name will raise some eyebrows amongst your fellow Python developers. Once you define a class, you can start instantiating it. In other words, you can start creating objects of that class. To do this, you’ll use a familiar syntax.
In Python, the class name provides what other languages, such as C++ and Java, call the class constructor. Calling a class, as you did with
Person, triggers Python’s class instantiation process, which internally runs in two steps. Firstly, create a new instance of the target class, and secondly, initialize the instance with suitable instance attribute values.
To continue with the example seen on-screen, the value that you pass as an argument to
Person is internally passed to
.__init__() and then assigned to the instance attribute
.name. In this way, you initialize your person instance,
john, with valid data, which you can confirm by accessing
John Doe is indeed his name. When you call the class to create a new instance, you need to provide as many arguments as
.__init__() requires so that this method can initialize all the instance attributes that demand an initial value.
It’s time to dig into another special method, called
.__new__(). This method takes care of creating new instances in Python. Note that the
.__new__() method is often called a class constructor in Python.
However, its job is actually to create new objects from the class blueprint, so you can more accurately call it an instance creator or object creator. The
.__new__() method takes the underlying class as its first argument and returns a new object. This object is typically an instance of the input class, in which case it will be handled by
.__init__() when it’s returned.
In some cases, however, it can be an instance of a different class, and in that case it will not be passed to
object class provides the base or default implementations of both
.__init__(), you rarely need to override
.__new__() in your custom classes. Most of the time, you can safely rely on the default implementation.
To summarize what you’ve learned so far, Python’s instantiation process starts when you call a class with appropriate arguments. Then the process runs through two steps, object creation with the
.__new__() method, and object initialization with the
Become a Member to join the conversation.