Subsections


2.1.7 Other Built-in Types

The interpreter supports several other kinds of objects. Most of these support only one or two operations.


2.1.7.1 Modules

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'>.


2.1.7.2 Classes and Class Instances

See chapters 3 and 7 of the Python Reference Manual for these.


2.1.7.3 Functions

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).


2.1.7.4 Methods

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.


2.1.7.5 Code Objects

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.


2.1.7.6 Type Objects

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'>.


2.1.7.7 The Null Object

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.


2.1.7.8 The Ellipsis Object

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.


2.1.7.9 File Objects

File objects are implemented using C's stdiopackage 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:

close ()
Close the file. A closed file cannot be read or written anymore.

flush ()
Flush the internal buffer, like stdio's fflush().

isatty ()
Return 1 if the file is connected to a tty(-like) device, else 0.

fileno ()
Return the integer ``file descriptor'' that is used by the underlying implementation to request I/O operations from the operating system. This can be useful for other, lower level interfaces that use file descriptors, e.g. module fcntl or os.read() and friends.

read ([size])
Read at most size bytes from the file (less if the read hits EOF before obtaining size bytes). If the size argument is negative or omitted, read all data until EOF is reached. The bytes are returned as a string object. An empty string is returned when EOF is encountered immediately. (For certain files, like ttys, it makes sense to continue reading after an EOF is hit.) Note that this method may call the underlying C function fread() more than once in an effort to acquire as close to size bytes as possible.

readline ([size])
Read one entire line from the file. A trailing newline character is kept in the string2.7 (but may be absent when a file ends with an incomplete line). If the size argument is present and non-negative, it is a maximum byte count (including the trailing newline) and an incomplete line may be returned. An empty string is returned when EOF is hit immediately. Note: Unlike stdio's fgets(), the returned string contains null characters ('\0') if they occurred in the input.

readlines ([sizehint])
Read until EOF using readline() and return a list containing the lines thus read. If the optional sizehint argument is present, instead of reading up to EOF, whole lines totalling approximately sizehint bytes (possibly after rounding up to an internal buffer size) are read.

seek (offset[, whence])
Set the file's current position, like 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.

tell ()
Return the file's current position, like stdio's ftell().

truncate ([size])
Truncate the file's size. If the optional size argument present, the file is truncated to (at most) that size. The size defaults to the current position. Availability of this function depends on the operating system version (for example, not all Unix versions support this operation).

write (str)
Write a string to the file. There is no return value. Note: Due to buffering, the string may not actually show up in the file until the flush() or close() method is called.

writelines (list)
Write a list of strings to the file. There is no return value. (The name is intended to match readlines(); writelines() does not add line separators.)

File objects also offer the following attributes:

closed
Boolean indicating the current state of the file object. This is a read-only attribute; the close() method changes the value.

mode
The I/O mode for the file. If the file was created using the open() built-in function, this will be the value of the mode parameter. This is a read-only attribute.

name
If the file object was created using open(), the name of the file. Otherwise, some string that indicates the source of the file object, of the form "<...>". This is a read-only attribute.

softspace
Boolean that indicates whether a space character needs to be printed before another value when using the print statement. Classes that are trying to simulate a file object should also have a writable softspace attribute, which should be initialized to zero. This will be automatic for classes implemented in Python; types implemented in C will have to provide a writable softspace attribute.


2.1.7.10 Internal Objects

See the Python Reference Manual for this information. It describes code objects, stack frame objects, traceback objects, and slice objects.



Footnotes

... string2.7
The advantage of leaving the newline on is that an empty string can be returned to mean EOF without being ambiguous. Another advantage is that (in cases where it might matter, e.g. if you want to make an exact copy of a file while scanning its lines) you can tell whether the last line of a file ended in a newline or not (yes this happens!).


Send comments on this document to python-docs@python.org.