Properties example | Sololearn: Learn to code for FREE!


Properties example

#Ilustration of atribute wrapping in # Python using set and get paradigm class Pizza: def __init__(self, tp): self.topp = tp self.cs=0 # set counter # get counter @property def Topping(self): return @Topping.setter def Topping(self, tp): self.cs+=1 self.topp = tp @Topping.getter def Topping(self): return self.topp pizza = Pizza("tomato") print("Topping:"+pizza.Topping) pizza.Topping="potato" print("Topping:"+pizza.Topping) #While it looks like simple access to # Topping atribute there is underlying # functionality to count number of # times atribute was changed or read print("Times set:{0}".format(pizza.cs)) print("Times get:{0}".format(

4/18/2020 11:07:39 AM

Uros Zivkovic

1 Answer

New Answer


Only post proper question in discussion area. Don't advertise snippets...