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
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: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.
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.
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.
Become a Member to join the conversation.