*Memo:
- My post explains an identifier.
- My post explains underscore (1).
- My post explains a variable assignment.
- My post explains a function (1).
A class name should be CapWords(PascalCase) as shown below:
class Cls: pass class MyCls: pass class MyFirstCls: pass A variable, function and parameter name should be lower_snake_case as shown below:
var = 'abc' my_var = 'abc' my_first_var = 'abc' def func(param): pass def my_func(my_param): pass def my_first_func(my_first_param): pass A constant name should be UPPER_SNAKE_CASE as shown below:
PI = 3.141592653589793 E = 2.718281828459045 MIN_NUM = 0 MAX_NUM = 100 MY_MIN_NUM = 0 MY_MAX_NUM = 100 A module name should be short and lower-case, and separated with underscores if improving readability as shown below:
my1stmod.py my_1st_mod.py myfirstmodule.py my_first_module.py import my1stmod import my_1st_mod import myfirstmodule import my_first_module A package name should be short and lower-case, and separated with underscores if really improving readability as shown below:
*Memo:
- Basically, underscores shouldn't be used.
my1stpkg my_1st_pkg myfirstpackage my_first_package import my1stpkg import my_1st_pkg import myfirstpackage import my_first_package A single, double and triple leading and trailing underscore can be used with variables, methods and classes as shown below:
*Memo:
- A single leading underscore(
_abc) can make the unforced private attributes(variables, methods and classes) in a class which are basically used in a class so they're still acceptable from the outside of the class. - A double leading underscore(
__abc) can make the forced private attributes in a class which are only used in the class so it's unacceptable from the outside of the class. - A double leading and trailing underscore(
__abc__) can make the special public attributes which are used in and out a class, e.g.__init__,__class__,__name__, etc. - A single trailing underscore(
abc_) is used to prevent identifier conflicts when using the identifiers (including reserved keywords) which already exist, e.g.var_,func_,len_,list_,int_,True_,def_,class_, etc.
class Cls: _var = "_var" # (Acceptable) __var = "__var" # (Unacceptable) var_ = "var_" # (Acceptable) __var__ = "__var__" # (Acceptable) def _func(self): pass # (Acceptable) def __func(self): pass # (Unacceptable) def func_(self): pass # (Acceptable) def __func__(self): pass # (Acceptable) class _Cls: pass # (Acceptable) class __Cls: pass # (Unacceptable) class Cls_: pass # (Acceptable) class __Cls__: pass # (Acceptable) cls = Cls() print(cls._var) # _var print(cls.__var) # Error print(cls.var_) # var_ print(cls.__var__) # __var__ print(cls._func.__name__) # _func print(cls.__func.__name__) # Error print(cls.func_.__name__) # func_ print(cls.__func__.__name__) # __func__ print(cls._Cls().__class__.__name__) # _Cls print(cls.__Cls().__class__.__name__) # Error print(cls.Cls_().__class__.__name__) # Cls_ print(cls.__Cls__().__class__.__name__) # __Cls__
Top comments (0)