What is the simplest way to define setter and getter in Python? Is there anything like in C#
public int Prop {get; set;}
How to make it like this? Since to write both setter and getter methods for one property like this is just too much work.
class MyClass():
def foo_get(self):
return self._foo
def foo_set(self, val):
self._foo = val
foo = property(foo_get, foo_set)
Thanks in advance!
If the setter and getter do nothing else than accessing an underlying real attribute, then the simplest way of implementing them is not to write setters and getters at all. This is the standard behaviour, and there is no point in writing functions recreating the behaviour the attribute has anyway.
You don’t need getters and setters to ensure encapsulation in the case your access logic changes to something different than the standard access mechanics later, since introducing a property won’t break your interface.
Python Is Not Java. (And not C# either, for that matter.)