Wednesday, November 4, 2015

private variable in python: comvention to use: _var_name ( _name, _age)

“Private” instance variables that cannot be accessed except from inside an object don’t exist in Python.

Generally private variable is accessible only from inside the object. 

Since there is a valid use-case for class-private members (namely to avoid name clashes of names with names defined by subclasses), there is limited support for such a mechanism, called name mangling. Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped. This mangling is done without regard to the syntactic position of the identifier, as long as it occurs within the definition of a class.

No comments:

Post a Comment