Object-oriented programming

Object-oriented programming

Explore the power of functions and object-oriented programming in Python. This section delves into function definitions, class structures, and advanced topics such as properties and descriptors, ensuring you understand how to create robust and reusable code.

16 audio · 4:11

Nortren·

What is a class in Python?

0:17
A class is a blueprint for creating objects. It defines attributes that describe the data and methods that describe the behavior. Instances of a class are objects with their own state. Classes are themselves objects, instances of the type metaclass, which means they can be assigned to variables and passed around.

What is the difference between a class and an instance?

0:14
A class defines structure and behavior. An instance is a concrete object created from the class, with its own data. A single class can have many instances. Class attributes are shared across all instances, while instance attributes are unique to each instance.

What is the dunder init method?

0:15
Dunder init is the initializer method called automatically when a new instance is created. It receives the new instance as self and can set initial attribute values. It is not technically a constructor; the actual construction is done by dunder new, which creates the instance before init runs.

What is the difference between dunder new and dunder init?

0:15
Dunder new is a static method that creates and returns a new instance. Dunder init initializes the already-created instance. New is rarely overridden except for immutable types or singletons. Init is overridden in almost every custom class to set up instance state.

What are dunder methods?

0:18
Dunder methods, short for double-underscore methods, are special methods recognized by Python that define how objects respond to built-in operations. Examples include dunder add for the plus operator, dunder len for the len function, and dunder str for string conversion. They are also called magic methods.

What is the difference between dunder str and dunder repr?

0:17
Dunder repr returns an unambiguous string representation, ideally one that could recreate the object. Dunder str returns a user-friendly string for display. The repr is used by interactive prompts and the repr function; the str is used by print and the str function. If only repr is defined, it is used as a fallback for str.

What is the difference between class attributes and instance attributes?

0:14
Class attributes are defined directly in the class body and are shared by all instances. Instance attributes are set on individual instances, usually in init via self. When you access an attribute, Python looks first on the instance, then on the class, then on its base classes.

What is inheritance?

0:15
Inheritance is a mechanism where a class derives attributes and methods from another class, called the parent or base. The derived class is called the subclass or child. Inheritance enables code reuse and polymorphism. A subclass can override inherited methods and add new ones.

What is multiple inheritance in Python?

0:16
Multiple inheritance is when a class inherits from more than one parent class. Python supports it directly. It allows a class to combine features from several sources but introduces complexity in method resolution. Python uses the C3 linearization algorithm to determine the order of method lookup.

What is the Method Resolution Order?

0:16
Method Resolution Order, or MRO, is the order in which Python looks up methods in a class hierarchy. With multiple inheritance, Python uses the C3 linearization algorithm to compute a consistent linear order. You can inspect it with the dunder mro attribute or the mro method on a class.

What does super do?

0:16
The super function returns a proxy object that delegates method calls to a parent or sibling class. It is most commonly used inside dunder init and other overridden methods to call the parent implementation. With multiple inheritance, super follows the MRO, which is essential for cooperative classes.

What is polymorphism in Python?

0:16
Polymorphism means that different types can respond to the same operation. In Python, this is achieved through duck typing: if an object provides the expected methods, it can be used in a given context regardless of its actual class. This is more flexible than the static polymorphism of typed languages.

What is duck typing?

0:15
Duck typing is the principle that an object's suitability is determined by whether it has the required methods and attributes, not by its class. The name comes from the saying that if it walks like a duck and quacks like a duck, it is a duck. Python embraces duck typing throughout its design.

What is encapsulation in Python?

0:17
Encapsulation is the practice of restricting direct access to an object's internal state, exposing only what is necessary. Python does not enforce access control; instead, it relies on naming conventions. A single leading underscore signals that an attribute is internal. A double leading underscore triggers name mangling.

What is name mangling?

0:14
Name mangling is when Python rewrites attribute names that start with two underscores but do not end with two underscores. The name is rewritten to include the class name, so dunder x in class A becomes underscore A double underscore x. This avoids accidental name clashes in subclasses.

What is the difference between an abstract method and a regular method?

0:16
An abstract method is declared but not implemented in a base class, requiring subclasses to provide an implementation. Python supports abstract methods through the abc module using the ABC base class and the abstractmethod decorator. A class with unimplemented abstract methods cannot be instantiated. ---