Python Property

Previous
Next

At property:

  • property() object is used to access the functionality of an object(class) easily
  • We need to create the property object in the definition of same class only.
  • Property object must be defined using arguments get, set and del methods and with optional argument.
  • We create the Property class object from outside of that class.
  • Using the property object associated with outer object, we access the functionality as follows.
class AtProperty: 
    def __init__(self, val): 
        self.__val = val
        return
          
    def getVal(self):
        print("Getting value")
        return self.__val 
          
    def setVal(self, val):
        print("Setting value")
        self.__val = val
        return
          
    def delVal(self):
        print("Deleting value")
        del self.__val
        return
      
    value = property(getVal, setVal, delVal, ) 
  
x = AtProperty('Python Language') 
print(x.value) # getting

x.value = 'Python'  # setting
print(x.value) # getting

del x.value  # deleting
print(x.value) # getting

Previous
Next

Add Comment

Courses Enquiry Form