Coming from the Java world, I am used to writing getter/setter for every member variables that I want to expose. (truth is I made use of IDE to auto generate)

I hate how much code Java has.

With it’s philosophy of readable code, Python is different.


To write a getter for this member foo:

class MyClass(object):
	def __init__(self):
		self._foo = 1

	def foo(self):
		return self._foo

It uses an annotation @property. Then to access the property:

my_class = MyClass()
# 1


To set a memter, use the annotation @foo.setter (replace foo with your member name).

class MyClass(object):

	def foo(self, value):
		self._foo = value




Back to Home