Properties example | Sololearn: Learn to code for FREE!

+1

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 self.cg=0 # get counter @property def Topping(self): return @Topping.setter def Topping(self, tp): self.cs+=1 self.topp = tp @Topping.getter def Topping(self): self.cg+=1 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(pizza.cg))

4/18/2020 11:07:39 AM

Uros Zivkovic

1 Answer

New Answer

+2

Only post proper question in discussion area. Don't advertise snippets... https://www.sololearn.com/discuss/1316935/?ref=app