Python property () function

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





We invite everyone to the two-day online intensive "Development of 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 for the required shares or currency. Join us!






The function is property()



used to define properties in classes.





The 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 #.





The method property()



takes on the input methods get



, set



and delete



, and returns class objects property



.





property()



property



(https://www.tutorialsteacher.com/python/property-decorator).





:





  1. fget



    : () . None. 





  2. fset



    : () . None.





  3. fdel



    : () . None.





  4. doc



    : () , . None.





:





, .





, Python 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)
      
      



property(getname, setname)



. , name



__name



. name



, getname()



setname()



, .





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



, getname()



, name



. setname



, name



. __name



.





, .





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)
      
      



delname()



, name



.





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



, property()



Python.





@property



property()



.






"Python Developer. Basic"





- « - Tkinter»: 1, 2.








All Articles