Python type() function
type() function returns the data type of the argument passed to it.
In Python, a class is a template for a data type. A class can be defined using the
Instantiate Python Class
In Python, a class needs to be instantiated before use.
As an analogy, a class can be thought of as a blueprint (Car), and an instance is an actual implementation of the blueprint (Ferrari).
__main__ in Python
__main__ is an identifier used to reference the current file context. When a module is read from standard input, a script, or from an interactive prompt, its
__name__ is set equal to
Suppose we create an instance of a class called
CoolClass. Printing the
type() of the instance will result in:
This means that the class
CoolClass was defined in the current script file.
Python Class Variables
In Python, class variables are defined outside of all methods and have the same value for every instance of the class.
Class variables are accessed with the
Python class methods
In Python, methods are functions that are defined as part of a class. It is common practice that the first argument of any method that is part of a class is the actual object calling the method. This argument is usually called self.
Python dir() function
In Python, the built-in
dir() function, without any argument, returns a list of all the attributes in the current scope.
With an object as argument,
dir() tries to return all valid object attributes.
__repr__() method is used to tell Python what the string representation of the class should be. It can only have one parameter,
self, and it should return a string.
Python init method
In Python, the
.__init__() method is used to initialize a newly created object. It is called every time the class is instantiated.
Subclassing in Python, also known as “inheritance”, allows classes to share the same attributes and methods from a parent or superclass. Inheritance in Python can be accomplished by putting the superclass name between parentheses after the subclass or child class name.
In the example code block, the
Dog class subclasses the
Animal class, inheriting all of its attributes.
User-defined exceptions in Python
In Python, new exceptions can be defined by creating a new class which has to be derived, either directly or indirectly, from Python’s Exception class.
Python issubclass() Function
issubclass() built-in function checks if the first argument is a subclass of the second argument.
In the example code block, we check that
Member is a subclass of the
Method Overriding in Python
In Python, inheritance allows for method overriding, which lets a child class change and redefine the implementation of methods already defined in its parent class.
The following example code block creates a
ParentClass and a
ChildClass which both define a
ChildClass inherits from the
ParentClass, the method
print_test() will be overridden by
ChildClasssuch that it prints the word “Child” instead of “Parent”.
Super() Function in Python Inheritance
super() function allows a subclass to invoke its parent’s version of an overridden method.
Polymorphism in Python
When two Python classes offer the same set of methods with different implementations, the classes are polymorphic and are said to have the same interface. An interface in this sense might involve a common inherited class and a set of overridden methods. This allows using the two objects in the same way regardless of their individual types.
When a child class overrides a method of a parent class, then the type of the object determines the version of the method to be called. If the object is an instance of the child class, then the child class version of the overridden method will be called. On the other hand, if the object is an instance of the parent class, then the parent class version of the method is called.
In Python, the
+ operation can be defined for a user-defined class by giving that class an
Dunder methods in Python
Dunder methods, which stands for “Double Under” (Underscore) methods, are special methods which have double underscores at the beginning and end of their names.
We use them to create functionality that can’t be represented as a normal method, and resemble native Python data type interactions. A few examples for dunder methods are:
The example code block shows a class with a definition for the
__init__ dunder method.