Python property () function

The translation of the material was prepared as part of the online course Python Developer. Basic

We invite everyone to a two-day online intensive “Developing a Desktop Application Using the Tkinter Library”… On the intensive course, we will get the initial skills of backend development in Python, as well as start developing a desktop application using the Tkinter library. At the end of 2 days, we will be able to create an investment application to view the current price of the required shares or currency. Join us!


Function property() used to define properties in classes.

Method property() provides an interface for the attributes of an instance of a class. It encapsulates instance attributes and provides properties, similar to how it works in Java and C #.

Method property() accepts methods as input get, set and delete, and returns objects of the class property

Instead of a method property() it’s better to use a decorator property (https://www.tutorialsteacher.com/python/property-decorator).

Parameters:

  1. fget: (optional) Function to get the value of an attribute. The default is None.

  2. fset: (optional) Function for setting the attribute value. The default is None.

  3. fdel: (optional) Function to remove the attribute value. The default is None.

  4. doc: (optional) A string containing the documentation. The default is None.

Returning values:

Returns the property attribute from the specified getter, setter, and delete function.

The following example shows how to create a property in Python using the function property()

class person:
    def __init__(self):
        self.__name=""
    def setname(self, name):
        print('setname() called')
        self.__name=name
    def getname(self):
        print('getname() called')
        return self.__name
    name=property(getname, setname)

In the example above property(getname, setname) returns a property object and gives it a name. Thus, the property name hides a private instance __name… Property access name carried out directly, but inside the method is called getname() or setname(), as shown below.

>>> from person import person
>>> p1=person()
>>> p1.name="Steve"
setname() called
>>> p1.name
getname() called
'Steve'

As seen above, the method getname() called automatically when we access the property name… Similarly, the method setname called when we assign a value to a property name… It hides the class attribute __name

Similarly, you can write a method for removing a property, as in the code below.

class person:
    def __init__(self, name):
        self.__name=name
    def setname(self, name):
        print('setname() called')
        self.__name=name
    def getname(self):
        print('getname() called')
        return self.__name
    def delname(self):
        print('delname() called')
        del self.__name
    # Set property to use get_name, set_name
    # and del_name methods
    name=property(getname, setname, delname)

Function delname() will be called when you remove the property name

>>> from person import person
>>> p1=person()
>>> p1.name="Steve"
setname() called
>>> del p1.name
delname() called

Thus, we can define a property in a class using the function property() in Python.

Decorator @property simplifies property declaration and allows you to do it without calling a function property()


Learn more about the course Python Developer. Basic

Registration for the two-day online intensive “Developing a Desktop Application Using the Tkinter Library”: Day 1, Day 2.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *