-
Notifications
You must be signed in to change notification settings - Fork 0
OOP Guidelines
Rahul Bachal edited this page Nov 8, 2015
·
10 revisions
All attributes are now private. To make an attribute of a class A public, there are two cases:
- Free read/write: If any object can read and modify an attribute of A, declare the attribute as self.attribute in A
- Read only: If any object can read an attribute of A but not modify it, declare the attribute as self._attribute and add a property for it. For example, to declare A._attribute as read only, do:
def __init__(self, **kwargs):
self._attribute = kwargs['attribute']
@property
def attribute(self):
return self._attribute
@attribute.setter
def attribute(self, value):
raise AttributeError("Cannot change A attribute")
To use private attributes within A, read and write them as A._attribute instead of using the property. Note: only do this internally.
To access a read only variable _attribute in an instance a of A, do:
a.attribute
Unless a method is called by an outside class other than Event, define it as private with an underscore.