Help again again | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Help again again

How does @property work in python

8th Jul 2023, 1:16 PM
Podle Playz
Podle Playz - avatar
4 Answers
+ 4
Maybe it helps if we ask the question what's the point of properties. After all, we can also simply create an instance variable, like this. class Pet: def __init__(self, name): self.name = name cat = Pet('Tom') print(cat.name) Python gives you a lot of freedom when you mess with the fields and attibutes of an object. It is difficult or impossible to block the user from changing some value. In classical OOP languages (like C++, Java or C#) encapsulation is a very important concept. This means that you only allow the user of your code to see or change only certain aspects of your objects. Typically this happens by defining getter and setter methods, which control how a particular field can be read or written. With property decorator you can achieve something similar in Python. This gives the developer some control over how the object's internal data can be consumed by other code.
8th Jul 2023, 2:41 PM
Tibor Santa
Tibor Santa - avatar
+ 2
https://realpython.com/JUMP_LINK__&&__python__&&__JUMP_LINK-property/ You can also find it in the Python Intermediate course, OOP lesson.
8th Jul 2023, 1:45 PM
Tibor Santa
Tibor Santa - avatar
+ 1
Tibor Santa Still, Its very confusing to me
8th Jul 2023, 1:55 PM
Podle Playz
Podle Playz - avatar