For more information on the REPL used in this course, you can check out bpython interpreter and the Real Python tutorial Discover bpython: A Python REPL With IDE-Like Features.
Introducing the Instantiation Process
00:00 The Instantiation Process.
Like many other programming languages, Python supports object-oriented programming. At the heart of Python’s object-oriented capabilities, you’ll find a
class keyword, which allows you to define custom classes that can have attributes for storing data and methods for providing behavior.
00:22 Once you have a class to work with, then you can start creating new instances or objects of that class, which is an efficient way to reuse functionality in your code.
00:32 Creating and initializing objects of a given class is a fundamental step in object-oriented programming. This step is often referred to as object construction or instantiation.
00:43 The tool responsible for running this instantiation process is commonly known as a class constructor.
00:50 In Python, to construct an object of a given class, you just need to call the class with appropriate arguments as you would call any function. In this course, you’ll see examples run using the Bpython REPL.
It offers a number of improvements over the standard Python REPL, including color coding. All of the examples seen here will run in the Python REPL, which you’ll usually access by typing
python at the command prompt.
In this example, you define
SomeClass using the
class keyword. This class is currently empty because it doesn’t have any attributes or methods.
Instead, the class’s body only contains a
pass statement as a placeholder statement that does nothing. Then you create a new instance of
SomeClass by calling the class with a pair of parentheses. In this example, you don’t need to pass any arguments in the call because your class doesn’t take any arguments yet. In Python, when you call a class such as seen in this example, you’re calling the class constructor, which creates, initializes, and returns a new object by triggering Python’s internal instantiation process.
A final point to note is that calling a class isn’t the same as calling an instance of a class. These are two different and unrelated topics. To make a class’s instance callable, you need to implement a
.__call__() special method, which has nothing to do with Python’s instantiation process.
02:23 You trigger Python’s instantiation process whenever you call a Python class to create a new instance. This process runs through two separate steps, which you can describe as follows.
Firstly, create the new instance of the target class. And secondly, initialize the new instance with an appropriate initial state. To run the first step, Python classes have a special method called
.__new__(), which is responsible for creating and returning a new empty object.
Then another special method,
.__init__(), takes the resulting object, along with a class constructor’s arguments. The
.__init__() method takes the new object as its first argument,
03:04 Then it sets any required instance attribute to a valid state using the arguments that the class constructor passed to it.
03:13 In the next section of the course, you’ll take a look at code which will explore the instantiation process in a practical example.
Become a Member to join the conversation.