Category: New Python tutorial

python tutorials and learn python

Created with Sketch.

Python Readonly Property

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…
Read more

Python Property Decorator

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: class property(fget=None, fset=None,…
Read more

Python Property

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: class Person: def __init__(self, name,…
Read more

Python __del__

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…
Read more

Python __bool__

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: class Person: def…
Read more

Python __hash__

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: class Person: def __init__(self, name, age): self.name =…
Read more

Python __eq__

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: class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name…
Read more

Python __repr__

Python __repr__ Summary: in this tutorial, you’ll learn how to use the Python __repr__ dunder method and the difference between the __repr__ and __str__ methods. Introduction to the Python __repr__ magic method The __repr__ dunder method defines behavior when you pass an instance of a class to the repr(). The __repr__ method returns the string…
Read more

Python __str__

Python __str__ Summary: in this tutorial, you’ll learn how to use the Python __str__ method to make a string representation of a class. Introduction to the Python __str__ method Let’s start with the Person class: class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age Code language: Python (python)…
Read more

Python Static Methods

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…
Read more