Class¶
1. Don’t access the private attributes from outside.¶
class A:
def __init__(self, name):
self._name = name
test = A()
print(test._name)
2. __init__ do not use return statement.¶
- Don’t
class A():
def __init__(self, name):
self.name = name
return
- Good
class A():
def __init__(self, name):
self.name = name
- use property instead of setter and getter.
- Using function instead of method.
When a method is not preceded by the @staticmethod or @classmethod decorators and does not contain any references to the class or instance (via keywords like cls or self)
- Add the @staticmethod decorator before the static method
- Add the @classmethod decorator before the class method