Instantiating Classes
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.
00:30
To define a class in Python, you need to use the class
keyword followed by the class name.
00:43 Python has a rich set of special methods that you can use in your classes. Python implicitly calls special methods to automatically execute a wide variety of operations on instances.
00:55 There are special methods to make your objects iterable, provide a suitable string representation, initialize instance attributes, and many more.
01:06
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 self
.
01:25
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.
01:41
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.
02:05
Call the class using a pair of parentheses (()
), which is the same syntax that you use to call any Python function.
02:14
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.
02:41
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 .name
.
03:00
Success! 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.
03:18 Now that you understand the object initialization mechanism, you are ready to learn what Python does before it gets to this point in the instantiation process.
03:27
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.
03:39
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.
04:02
In some cases, however, it can be an instance of a different class, and in that case it will not be passed to .__init__()
. Python’s object
class provides the base or default implementations of both .__new__()
.__init__()
. Unlike .__init__()
, you rarely need to override .__new__()
in your custom classes. Most of the time, you can safely rely on the default implementation.
04:28
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 .__init__()
method.
04:45 Now that you know about this internal behavior of Python, you are ready to dive into providing multiple constructs in your classes, and that’s what you’ll be looking at in the next section.
Become a Member to join the conversation.