The interpreter supports several other kinds of objects. Most of these support only one or two operations.
The only special operation on a module is attribute access:
m.name
, where m is a module and name
accesses a name defined in m's symbol table. Module attributes
can be assigned to. (Note that the import statement is not,
strictly speaking, an operation on a module object; import
foo
does not require a module object named foo to exist,
rather it requires an (external) definition for a module named
foo somewhere.)
A special member of every module is __dict__.
This is the dictionary containing the module's symbol table.
Modifying this dictionary will actually change the module's symbol
table, but direct assignment to the __dict__ attribute is not
possible (i.e., you can write m.__dict__['a'] = 1
, which
defines m.a
to be 1
, but you can't write
m.__dict__ = {}
.
Modules built into the interpreter are written like this:
<module 'sys' (built-in)>
. If loaded from a file, they are
written as <module 'os' from '/usr/local/lib/python1.5/os.pyc'>
.
See chapters 3 and 7 of the Python Reference Manual for these.
Function objects are created by function definitions. The only
operation on a function object is to call it:
func(argument-list)
.
There are really two flavors of function objects: built-in functions and user-defined functions. Both support the same operation (to call the function), but the implementation is different, hence the different object types.
The implementation adds two special read-only attributes:
f.func_code
is a function's code
object (see below) and f.func_globals
is
the dictionary used as the function's global name space (this is the
same as m.__dict__
where m is the module in which
the function f was defined).
Methods are functions that are called using the attribute notation. There are two flavors: built-in methods (such as append() on lists) and class instance methods. Built-in methods are described with the types that support them.
The implementation adds two special read-only attributes to class
instance methods: m.im_self
is the object on which the
method operates, and m.im_func
is the function
implementing the method. Calling m(arg-1,
arg-2, ..., arg-n)
is completely equivalent to
calling m.im_func(m.im_self, arg-1,
arg-2, ..., arg-n)
.
See the Python Reference Manual for more information.
Code objects are used by the implementation to represent ``pseudo-compiled'' executable Python code such as a function body. They differ from function objects because they don't contain a reference to their global execution environment. Code objects are returned by the built-in compile() function and can be extracted from function objects through their func_code attribute.
A code object can be executed or evaluated by passing it (instead of a source string) to the exec statement or the built-in eval() function.
See the Python Reference Manual for more information.
Type objects represent the various object types. An object's type is accessed by the built-in function type(). There are no special operations on types. The standard module types defines names for all standard built-in types.
Types are written like this: <type 'int'>
.
This object is returned by functions that don't explicitly return a
value. It supports no special operations. There is exactly one null
object, named None
(a built-in name).
It is written as None
.
This object is used by extended slice notation (see the Python
Reference Manual). It supports no special operations. There is
exactly one ellipsis object, named Ellipsis
(a built-in name).
It is written as Ellipsis
.
File objects are implemented using C's stdio
package and can be created with the built-in function
open() described in section
2.3, ``Built-in Functions.'' They are also returned
by some other built-in functions and methods, e.g.,
posix.popen() and posix.fdopen() and the
makefile() method of socket objects.
When a file operation fails for an I/O-related reason, the exception IOError is raised. This includes situations where the operation is not defined for some reason, like seek() on a tty device or writing a file opened for reading.
Files have the following methods:
stdio
's fflush().
1
if the file is connected to a tty(-like) device, else
0
.
stdio
's fgets(), the returned
string contains null characters ('\0'
) if they occurred in the
input.
stdio
's fseek().
The whence argument is optional and defaults to 0
(absolute file positioning); other values are 1
(seek
relative to the current position) and 2
(seek relative to the
file's end). There is no return value.
stdio
's
ftell().
File objects also offer the following attributes:
See the Python Reference Manual for this information. It describes code objects, stack frame objects, traceback objects, and slice objects.