Category: 1. Classes and objects

  • Python Static Methods

    Summary: in this tutorial, you’ll learn about Python static methods and how to use them to create a utility class. Introduction to Python static methods So far, you have learned about instance methods that are bound to a specific instance. It means that instance methods can access and modify the state of the bound object.…

  • Python Class Attributes

    Summary: in this tutorial, you’ll learn about the Python class attributes and when to use them appropriately. Introduction to class attributes Let’s start with a simple Circle class: The Circle class has two attributes pi and radius. It also has two methods that calculate the area and circumference of a circle. Both pi and radius are called instance attributes. In other words, they belong to a specific…

  • Python Private Attributes

    Summary: in this tutorial, you’ll learn about encapsulation and how to use private attributes to accomplish encapsulation in Python. Introduction to encapsulation in Python Encapsulation is one of the four fundamental concepts in object-oriented programming including abstraction, encapsulation, inheritance, and polymorphism. Encapsulation is the packing of data and functions that work on that data within a single…

  • Python Instance Variables

    Summary: in this tutorial, you’ll learn about Python instance variables including data variables and function variables. Introduction to the Python instance variables In Python, class variables are bound to a class while instance variables are bound to a specific instance of a class. The instance variables are also called instance attributes. The following defines a HtmlDocument class with two class variables:…

  • Python __init__

    Summary: in this tutorial, you’ll learn how to use the Python __init__() method to initialize object’s attributes. Introduction to the Python __init__() method When you create a new object of a class, Python automatically calls the __init__() method to initialize the object’s attributes. Unlike regular methods, the __init__() method has two underscores (__) on each side. Therefore, the __init__() is often called dunder init. The name…

  • Python Methods

    Summary: in this tutorial, you’ll learn about Python methods and the differences between functions and methods. Introduction to the Python methods By definition, a method is a function that is bound to an instance of a class. This tutorial helps you understand how it works under the hood. The following defines a Request class that contains a function send(): And you…

  • Python Class Variables

    Summary: in this tutorial, you’ll learn how the Python class variables (or attributes) work. Introduction to the Python class variables Everything in Python is an object including a class. In other words, a class is an object in Python. When you define a class using the class keyword, Python creates an object with the name the same as…

  • Python Class

    Summary: in this tutorial, you’ll learn about Python classes and objects and how to define a new class. Objects An object is a container that contains data and functionality. The data represents the object at a particular moment in time. Therefore, the data of an object is called the state. Python uses attributes to model the state of an object. The functionality represents…

  • Python Object-oriented Programming

    Summary: in this tutorial, you’ll learn object-oriented programming in Python, including essential concepts such as objects, classes, attributes, methods, inheritances, overriding methods, etc. Introduction to Python Object-oriented Programming Everything in Python is an object. An object has a state and behaviors. To create an object, you define a class first. And then, from the class,…