which approach is most efficient for design a program in Python: property function or property decorator? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

which approach is most efficient for design a program in Python: property function or property decorator?

7th Aug 2019, 12:31 AM
Pavel Bellorín
Pavel Bellorín - avatar
2 Answers
+ 1
Property function and property decorator do the exact same thing. They provide a way to define custom behavior or rules when a class attribute is retrieved (get) or changed (set). While this is the norm of encapsulation in many OOP languages, python is a bit different because you cannot really hide the internal workings of a class, technically everything remains public. So unless you really have to apply some special logic or restriction on the value of an attribute, it is not needed to implement getters and setters. If you really must, then I think decorators are more pythonic :) https://www.programiz.com/python-programming/property
8th Aug 2019, 1:19 PM
Tibor Santa
Tibor Santa - avatar
0
Her... Aren't decorators a type of function? (or am I off track?)
8th Aug 2019, 12:02 PM
Gwlanbzh
Gwlanbzh - avatar