GNU Info

Info Node: (python2.1-ref.info)Special method names

(python2.1-ref.info)Special method names


Prev: standard type hierarchy Up: Data model
Enter node , (file) or (file)node

Special method names
====================

A class can implement certain operations that are invoked by special
syntax (such as arithmetic operations or subscripting and slicing) by
defining methods with special names.  For instance, if a class defines
a method named `__getitem__()', and `x' is an instance of this class,
then `x[i]' is equivalent to `x.__getitem__(i)'.  (The reverse is not
true -- if `x' is a list object, `x.__getitem__(i)' is not equivalent to
`x[i]'.)  Except where mentioned, attempts to execute an operation
raise an exception when no appropriate method is defined.

When implementing a class that emulates any built-in type, it is
important that the emulation only be implemented to the degree that it
makes sense for the object being modelled.  For example, some sequences
may work well with retrieval of individual elements, but extracting a
slice may not make sense.  (One example of this is the `NodeList'
interface in the W3C's Document Object Model.)

Basic customization
Customizing attribute access
Emulating callable objects
Emulating sequence and mapping types
Additional methods for emulation of sequence types
Emulating numeric types

automatically generated by info2www version 1.2.2.9