Author: admin

  • Python Overriding Method

    Summary: in this tutorial, you’ll learn how to use Python overriding method to allow a child class to provide a specific implementation of a method that is provided by one of its parent classes. Introduction to Python overridding method The overriding method allows a child class to provide a specific implementation of a method that…

  • Python Inheritance

    Summary: in this tutorial, you’ll learn about Python inheritance and how to use the inheritance to reuse code from an existing class. Introduction to the Python inheritance Inheritance allows a class to reuse the logic of an existing class. Suppose you have the following Person class: The Person class has the name attribute and the greet() method. Now, you want to define the Employee that is similar…

  • Python Delete Property

    Summary: in this tutorial, you’ll learn how to use the property() class to delete the property of an object. To create a property of a class, you use the @property decorator. Underhood, the @property decorator uses the property class that has three methods: setter, getter, and deleter. By using the deleter, you can delete a property from an object. Notice that the deleter() method…

  • Python Readonly Property

    Summary: in this tutorial, you’ll learn how to define Python readonly property and how to use it to define computed properties. Introduction to the Python readonly property To define a readonly property, you need to create a property with only the getter. However, it is not truly read-only because you can always access the underlying attribute…

  • Python Property Decorator

    Summary: in this tutorial, you’ll learn about Python property decorator (@property) and more importantly how it works. Introduction to the Python property decorator In the previous tutorial, you learned how to use the property class to add a property to a class. Here’s the syntax of the property class: The following defines a Person class with two attributes name and age: To define a…

  • Python Property

    Summary: in this tutorial, you’ll learn about the Python property class and how to use it to define properties for a class. Introduction to class properties The following defines a Person class that has two attributes name and age, and create a new instance of the Person class: Since age is the instance attribute of the Person class, you can assign it a new value like this: The following assignment is also…

  • Python __del__

    Summary: in this tutorial, you will learn about the Python __del__ special method and understand how it works. Introduction to the Python __del__ method In Python, the garbage collector manages memory automatically. The garbage collector will destroy the objects that are not referenced. If an object implements the __del__ method, Python calls the __del__ method right before the garbage collector destroys the object. However, the garbage…

  • Python __bool__

    Summary: in this tutorial, you will learn how to implement the Python __bool__ method to return boolean values for objects of a custom class. Introduction to the Python __bool__ method An object of a custom class is associated with a boolean value. By default, it evaluates to True. For example: In this example, we define the Person class, instantiate an object, and…

  • Python __hash__

    Summary: in this tutorial, you’ll learn about the Python hash() function and how to override the __hash__ method in a custom class. Introduction to the Python hash function Let’s start with a simple example. First, define the Person class with the name and age attributes: Second, create two instances of the Person class: Third, show the hashes of the p1 and p2 objects: Output: The hash() function accepts an object and returns…

  • Python __eq__

    Summary: in this tutorial, you’ll learn how to use the Python __eq__ method to compare two objects by their values. Introduction to the Python __eq__ method Suppose that you have the following Person class with three instance attributes: first_name, last_name, and age: And you create two instances of the Person class: In this example, the john and jane objects are not the same object. And you can check it using…