Properties example | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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))

18th Apr 2020, 11:07 AM
Uros Zivkovic
Uros Zivkovic - avatar
1 Answer
+ 2
Only post proper question in discussion area. Don't advertise snippets... https://www.sololearn.com/discuss/1316935/?ref=app
19th Apr 2020, 3:07 PM
Arushi Singhania
Arushi Singhania - avatar