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
  1. use property instead of setter and getter.
  2. 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)

  1. Add the @staticmethod decorator before the static method
  2. Add the @classmethod decorator before the class method

3. Do not dynamically creating variable.