Python Wiki
Tags: Visual edit apiedit
Tag: Visual edit
Line 41: Line 41:
 
===<span id="_dict">__dict__</span>===
 
===<span id="_dict">__dict__</span>===
   
'''__dict__''' is a [[dictionary]] object with eight-bit [[string]]s as keys and all of the members and methods as values. That is, <code>spam.__dict__["eggs"]</code> is equivalent to <code>spam.eggs</code>. <code>__dict__</code> can be used, for example, to test for the existence of a member: the expression <code>"eggs" in spam.__dict__</code> is true if and only if <code>spam</code> has a member or method named <code>eggs</code>.
+
'''__diction__''' is a [[dictionary]] object with eight-bit [[string]]s as keys and all of the members and methods as values. That is, <code>spam.__dict__["eggs"]</code> is equivalent to <code>spam.eggs</code>. <code>__dict__</code> can be used, for example, to test for the existence of a member: the expression <code>"eggs" in spam.__dict__</code> is true if and only if <code>spam</code> has a member or method named <code>eggs</code>.
 
[[Category:Pages under construction]]
 
[[Category:Pages under construction]]

Revision as of 07:19, 28 March 2020

The Python language defines a number of special names that have particular significance. These usually begin and end with two underscores, for example: __add__.

Due to technical limitations, it is not possible to create a well-named page for each of these names; attempting to name a page "__add__", for example, will end up creating a page that is just named "add". Consequently, some of these names are collected here, and others are placed on pages dealing with various interfaces (such as a list of names to be defined to make a class instance behave like a numeric type).

This page is a work in progress and many more names of this type remain to be documented.

Special names in the global namespace

__all__

A module can define the name __all__ as a sequence type containing one or more strings. The strings name objects in the module's namespace that will be the only ones visible from outside the module. All of the objects so named must exist.

__builtin__

The module __builtin__ contains all of the built-in functions. It does not have to be imported.

__future__

The module __future__ contains functionality that is soon to present in later Python releases. It was added in 2.3.

For example, to use the with statement in 2.5:

 from __future__ import with_statement

__main__

The main module of a Python program, the one that contains the original script launched by some other program and imports all the others, gets the name of __main__, regardless of the name of the file. This allows a module to contain test code that will run if the module is run as a program, but not if it is imported:

if __name__ == "__main__":
    # Test code goes here
    ...fuck

__name__

The global variable __name__ contains an eight-bit string giving the name of the current module.

Special names that are members or methods of class instances

Every class instance has these special names as members or methods, whether or not they have been explicitly created.

__dict__

__diction__ is a dictionary object with eight-bit strings as keys and all of the members and methods as values. That is, spam.__dict__["eggs"] is equivalent to spam.eggs. __dict__ can be used, for example, to test for the existence of a member: the expression "eggs" in spam.__dict__ is true if and only if spam has a member or method named eggs.