(continues on next page) Chapter 9. Classes 74 Python Tutorial, Release 3.9.5 9.6 Private Variables 9.7 Odds and Ends “Private” instance variables that cannot be accessed except from inside an object don’t exist in Python. However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g. _spam ) should be treated as a non-public part of the API (whether it is a function, a method or a data member). It should be considered an implementation detail and subject to change without notice. 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. Name mangling is helpful for letting subclasses override methods without breaking intraclass method calls. For example: classMapping : def __init__ (self, iterable): self.items_list=[] self.__update(iterable) def update(self, iterable): for item in iterable: self.items_list.append(item) __update=update # private copy of original update() method classMappingSubclass (Mapping): def update(self, keys, values): # provides new signature for update() # but does not break __init__() for item in zip (keys, values): self.items_list.append(item) The above example would work even if$_{MappingSubclass}$were to introduce a$_{__update}$ identifier since it is replaced with _Mapping__update in the Mapping class and _MappingSubclass__update in the $_{MappingSubclass}$class respectively. Note that the mangling rules are designed mostly to avoid accidents; it still is possible to access or modify a variable that is considered private. This can even be useful in special circumstances, such as in the debugger. Notice that code passed to$_{exec()}$ or$_{eval()}$ does not consider the classname of the invoking class to be the current class; this is similar to the effect of the$_{global}$ statement, the effect of which is likewise restricted to code that is byte-compiled together. The same restriction applies to$_{getattr()}$ ,$_{setattr()}$ and$_{delattr()}$ , as well as when referencing$_{__dict__}$ directly. Sometimes it is useful to have a data type similar to the Pascal “record” or C “struct”, bundling together a few named data items. An empty class definition will do nicely: classEmployee : pass john =Employee() # Create an empty employee record # Fill the fields of the record