Python Recipes Handbook by Joey Bernard

Python Recipes Handbook by Joey Bernard

Author:Joey Bernard
Language: eng
Format: epub
Publisher: Apress, Berkeley, CA


Defining an attribute that is meant to be only used within the class in question should be prefixed with at least one underscore character. The usual way this is done is to actually add two underscore characters to both the beginning and ending of the element names, as in Listing 7-5.

class priv_vars:

__a__ = "This is a private variable "

Listing 7-5.Creating a Private Variable

There is a special form of a private name that induces Python to rename the element using a system of name mangling. If you have an element that may have the same name as some other element, you can add at least two leading underscore characters and at most one trailing underscore. Python will then prepend the class name to the element name. For example, if you had a class like

class my_class1:

__a_ = 1



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.