Skip to main content

The generally accepted way of doing things is just using simple attributes, like so

>>> class MyClass:
...     myAttribute = 0
... 
>>> c = MyClass()
>>> c.myAttribute 
0
>>> c.myAttribute = 1
>>> c.myAttribute
1

If you do find yourself needing to be able to write getters and setters, then what you want to look for is "python class properties" and thisRyan Tomayko's article on Getters/Setters/Fuxors is a great place to start (albeit a little long)

The generally accepted way of doing things is just using simple attributes, like so

>>> class MyClass:
...     myAttribute = 0
... 
>>> c = MyClass()
>>> c.myAttribute 
0
>>> c.myAttribute = 1
>>> c.myAttribute
1

If you do find yourself needing to be able to write getters and setters, then what you want to look for is "python class properties" and this article is a great place to start (albeit a little long)

The generally accepted way of doing things is just using simple attributes, like so

>>> class MyClass:
...     myAttribute = 0
... 
>>> c = MyClass()
>>> c.myAttribute 
0
>>> c.myAttribute = 1
>>> c.myAttribute
1

If you do find yourself needing to be able to write getters and setters, then what you want to look for is "python class properties" and Ryan Tomayko's article on Getters/Setters/Fuxors is a great place to start (albeit a little long)

Source Link
willurd
  • 12.1k
  • 5
  • 31
  • 23

The generally accepted way of doing things is just using simple attributes, like so

>>> class MyClass:
...     myAttribute = 0
... 
>>> c = MyClass()
>>> c.myAttribute 
0
>>> c.myAttribute = 1
>>> c.myAttribute
1

If you do find yourself needing to be able to write getters and setters, then what you want to look for is "python class properties" and this article is a great place to start (albeit a little long)