Key Concepts

Review core concepts you need to learn to master this subject

Python type() function

a = 1 print type(a) # <type 'int'> a = 1.1 print type(a) # <type 'float'> a = 'b' print type(a) # <type 'str'> a = None print type(a) # <type 'NoneType'>

The Python type() function returns the data type of the argument passed to it.

Python class

a = 1 print type(a) # <type 'int'> a = 1.1 print type(a) # <type 'float'> a = 'b' print type(a) # <type 'str'> a = None print type(a) # <type 'NoneType'>

In Python, a class is a template for a data type. A class can be defined using the class keyword.

Instantiate Python Class

a = 1 print type(a) # <type 'int'> a = 1.1 print type(a) # <type 'float'> a = 'b' print type(a) # <type 'str'> a = None print type(a) # <type 'NoneType'>

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

a = 1 print type(a) # <type 'int'> a = 1.1 print type(a) # <type 'float'> a = 'b' print type(a) # <type 'str'> a = None print type(a) # <type 'NoneType'>

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 __main__.

Suppose we create an instance of a class called CoolClass. Printing the type() of the instance will result in:

<class '__main__.CoolClass'>

This means that the class CoolClass was defined in the current script file.

Python Class Variables

a = 1 print type(a) # <type 'int'> a = 1.1 print type(a) # <type 'float'> a = 'b' print type(a) # <type 'str'> a = None print type(a) # <type 'NoneType'>

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 instance.variable or class_name.variable syntaxes.

Python class methods

a = 1 print type(a) # <type 'int'> a = 1.1 print type(a) # <type 'float'> a = 'b' print type(a) # <type 'str'> a = None print type(a) # <type 'NoneType'>

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

a = 1 print type(a) # <type 'int'> a = 1.1 print type(a) # <type 'float'> a = 'b' print type(a) # <type 'str'> a = None print type(a) # <type 'NoneType'>

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.

Python repr method

a = 1 print type(a) # <type 'int'> a = 1.1 print type(a) # <type 'float'> a = 'b' print type(a) # <type 'str'> a = None print type(a) # <type 'NoneType'>

The Python __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

a = 1 print type(a) # <type 'int'> a = 1.1 print type(a) # <type 'float'> a = 'b' print type(a) # <type 'str'> a = None print type(a) # <type 'NoneType'>

In Python, the .__init__() method is used to initialize a newly created object. It is called every time the class is instantiated.

Python Inheritance

a = 1 print type(a) # <type 'int'> a = 1.1 print type(a) # <type 'float'> a = 'b' print type(a) # <type 'str'> a = None print type(a) # <type 'NoneType'>

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.

Python Custom Exceptions

a = 1 print type(a) # <type 'int'> a = 1.1 print type(a) # <type 'float'> a = 'b' print type(a) # <type 'str'> a = None print type(a) # <type 'NoneType'>

In Python, a new Exception can be defined by creating a class that inherits from Python’s Exception class.

User-defined exceptions in Python

a = 1 print type(a) # <type 'int'> a = 1.1 print type(a) # <type 'float'> a = 'b' print type(a) # <type 'str'> a = None print type(a) # <type 'NoneType'>

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

a = 1 print type(a) # <type 'int'> a = 1.1 print type(a) # <type 'float'> a = 'b' print type(a) # <type 'str'> a = None print type(a) # <type 'NoneType'>

The Python 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 Family class.

Method Overriding in Python

a = 1 print type(a) # <type 'int'> a = 1.1 print type(a) # <type 'float'> a = 'b' print type(a) # <type 'str'> a = None print type(a) # <type 'NoneType'>

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 print_test() method.

As the 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

a = 1 print type(a) # <type 'int'> a = 1.1 print type(a) # <type 'float'> a = 'b' print type(a) # <type 'str'> a = None print type(a) # <type 'NoneType'>

Python’s super() function allows a subclass to invoke its parent’s version of an overridden method.

Polymorphism in Python

a = 1 print type(a) # <type 'int'> a = 1.1 print type(a) # <type 'float'> a = 'b' print type(a) # <type 'str'> a = None print type(a) # <type 'NoneType'>

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.

Dunder methods in Python

a = 1 print type(a) # <type 'int'> a = 1.1 print type(a) # <type 'float'> a = 'b' print type(a) # <type 'str'> a = None print type(a) # <type 'NoneType'>

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: __init__, __add__, __len__, and __iter__.

The example code block shows a class with a definition for the __init__ dunder method.

Chevron Left Icon
Learn Python: Classes
Lesson 1 of 2
Chevron Right Icon
  1. 1

    Python equips us with many different ways to store data. A […] is a different kind of number from an […] , and we store different data in a […] than we do in a […] . These are known a…

  2. 2

    A class is a template for a data type. It describes the kinds of information that class will hold and how a programmer will interact with that data. Define a class using the […] keyword. [PEP…

  3. 3

    A class doesn’t accomplish anything simply by being defined. A class must be instantiated. In other words, we must create an instance of the class, in order to breathe life into the schematic. …

  4. 4

    A class instance is also called an object. The pattern of defining classes and creating objects to represent the responsibilities of a program is known as Object Oriented Programming or OOP. I…

  5. 5

    When we want the same data to be available to every instance of a class we use a class variable. A class variable is a variable that’s the same for every instance of the class. You can define a…

  6. 6

    Methods are functions that are defined as part of a class. The first argument in a method is always the object that is calling the method. Convention recommends that we name this first argument …

  7. 7

    Methods can also take more arguments than just […] : […] Above we defined a […] class, instantiated it, and used it to convert 5 miles into kilometers. Notice again that even though […

  8. 8

    There are several methods that we can define in a Python class that have special behavior. These methods are sometimes called “magic”, because they behave differently from regular methods. Another …

  9. 9

    We’ve learned so far that a class is a schematic for a data type and an object is an instance of a class, but why is there such a strong need to differentiate the two if each object can only have t…

  10. 10

    Instance variables and class variables are both accessed similarly in Python. This is no mistake, they are both considered attributes of an object. If we attempt to access an attribute that is ne…

  11. 11

    Since we can already use dictionaries to store key-value pairs, using objects for that purpose is not really useful. Instance variables are more powerful when you can guarantee a rigidity to the da…

  12. 12

    Attributes can be added to user-defined objects after instantiation, so it’s possible for an object to have some attributes that are not explicitly defined in an object’s constructor. We can use th…

  13. 13

    One of the first things we learn as programmers is how to print out information that we need for debugging. Unfortunately, when we print out an object we get a default representation that seems fai…

  14. 14

    So far we’ve covered what a data type actually is in Python. We explored what the functionality of Python’s built-in types (also referred to as primitives) are. We learned how to create our own d…

  1. 1

    Classes are designed to allow for more code reuse, but what if we need a class that looks a lot like a class we already have? If the bulk of a class’s definition is useful, but we have a new use ca…

  2. 2

    There’s one very important family of class definitions built in to the Python language. An Exception is a class that inherits from Python’s […] class. We can validate this ourselves using th…

  3. 3

    Inheritance is a useful way of creating objects with different class variables, but is that all it’s good for? What if one of the methods needs to be implemented differently? In Python, all we have…

  4. 4

    Overriding methods is really useful in some cases but sometimes we want to add some extra logic to the existing method. In order to do that we need a way to call the method from the parent class. P…

  5. 5

    You may be wondering at this point why we would even want to have two different classes with two differently implemented methods to use the same method name. This style is especially useful when we…

  6. 6

    All this talk of interfaces demonstrates flexibility in programming. Flexibility in programming is a broad philosophy, but what’s worth remembering is that we want to implement forms that are famil…

  7. 7

    One way that we can introduce polymorphism to our class definitions is by using Python’s special dunder methods. We’ve explored a few already, the constructor […] and the string representation …

  8. 8

    Python offers a whole suite of magic methods a class can implement that will allow us to use the same syntax as Python’s built-in data types. You can write functionality that allows custom defined …

  9. 9

    In this lesson, we learned more complicated relationships between classes. We learned: How to create a subclass of an existing class. How to redefine existing methods of a parent class in a su…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo