Module ImportFrom alias Import alias ClassDef bases NameLoad body FunctionDef arguments args NameParam NameParam defaults NameLoad body Expr Call AttributeLoad NameLoad attr NameLoad Assign AttributeStore NameLoad attr NameLoad decorator_list FunctionDef arguments args NameParam NameParam defaults body TryExcept body Return Call AttributeLoad NameLoad attr NameLoad NameLoad handlers ExceptHandler type NameLoad body Return AttributeLoad NameLoad attr decorator_list FunctionDef arguments args NameParam NameParam defaults vararg body If UnaryOpNot NameLoad body Assign NameStore TupleLoad AttributeLoad NameLoad attr Return Call AttributeLoad NameLoad attr NameLoad NameLoad NameLoad decorator_list FunctionDef arguments args NameParam NameParam defaults body For NameStore NameLoad body If CompareNotIn NameLoad NameLoad body Assign SubscriptStore NameLoad Index NameLoad SubscriptLoad NameLoad Index NameLoad decorator_list decorator_list Assign NameStore Str ClassDef bases NameLoad body Assign NameStore ListLoad Str FunctionDef arguments args NameParam NameParam defaults NameLoad body Expr Call AttributeLoad NameLoad attr NameLoad Assign AttributeStore NameLoad attr NameLoad decorator_list FunctionDef arguments args NameParam NameParam defaults body TryExcept body Return Call AttributeLoad NameLoad attr NameLoad NameLoad handlers ExceptHandler type NameLoad body Return AttributeLoad NameLoad attr decorator_list FunctionDef arguments args NameParam NameParam defaults vararg body If UnaryOpNot NameLoad body Assign NameStore TupleLoad AttributeLoad NameLoad attr Return Call AttributeLoad NameLoad attr NameLoad NameLoad NameLoad decorator_list FunctionDef arguments args NameParam NameParam defaults body For NameStore NameLoad body If CompareNotIn NameLoad NameLoad body Assign SubscriptStore NameLoad Index NameLoad SubscriptLoad NameLoad Index NameLoad decorator_list decorator_list Assign NameStore Str Assign NameStore Str Assign NameStore Str Assign NameStore Str Assign NameStore Str ClassDef bases NameLoad body FunctionDef arguments args NameParam defaults body Return Str decorator_list decorator_list ClassDef bases NameLoad body FunctionDef arguments args NameParam defaults body Return BinOpAdd Str Call AttributeLoad Call NameLoad NameLoad NameLoad attr decorator_list decorator_list ClassDef bases NameLoad body FunctionDef arguments args NameParam defaults body Return BinOpAdd Str Call AttributeLoad Call NameLoad NameLoad NameLoad attr decorator_list decorator_list ClassDef bases NameLoad NameLoad body FunctionDef arguments args NameParam defaults body Return BinOpAdd Str Call AttributeLoad Call NameLoad NameLoad NameLoad attr decorator_list decorator_list Assign NameStore Str Assign NameStore Str Assign NameStore Dict Str Str Str Str Str Str Str Str NameLoad NameLoad NameLoad NameLoad NameLoad NameLoad NameLoad NameLoad FunctionDef arguments args NameParam defaults NameLoad body ImportFrom alias alias Expr Call AttributeLoad NameLoad attr NameLoad NameLoad decorator_list If CompareEq NameLoad Str body Expr Call NameLoad Num
N/A test.test_support sortdict N/A pprint defaultdict N/A dict N/A __init__ N/A N/A self default N/A None N/A N/A N/A N/A dict __init__ self N/A N/A self default default N/A __getitem__ N/A N/A self key N/A N/A N/A N/A N/A N/A N/A dict __getitem__ self key N/A N/A N/A KeyError N/A N/A N/A self default N/A get N/A N/A self key N/A args N/A N/A N/A args N/A N/A args N/A N/A self default N/A N/A N/A dict get self key args N/A merge N/A N/A self other N/A N/A N/A key other N/A N/A N/A key self N/A N/A N/A self N/A key N/A other N/A key N/A N/A N/A test_1 

Here's the new type at work:

    >>> print defaultdict               # show our type
    <class 'test.test_descrtut.defaultdict'>
    >>> print type(defaultdict)         # its metatype
    <type 'type'>
    >>> a = defaultdict(default=0.0)    # create an instance
    >>> print a                         # show the instance
    {}
    >>> print type(a)                   # show its type
    <class 'test.test_descrtut.defaultdict'>
    >>> print a.__class__               # show its class
    <class 'test.test_descrtut.defaultdict'>
    >>> print type(a) is a.__class__    # its type is its class
    True
    >>> a[1] = 3.25                     # modify the instance
    >>> print a                         # show the new value
    {1: 3.25}
    >>> print a[1]                      # show the new item
    3.25
    >>> print a[0]                      # a non-existent item
    0.0
    >>> a.merge({1:100, 2:200})         # use a dict method
    >>> print sortdict(a)               # show the result
    {1: 3.25, 2: 200}
    >>>

We can also use the new type in contexts where classic only allows "real"
dictionaries, such as the locals/globals dictionaries for the exec
statement or the built-in function eval():

    >>> def sorted(seq):
    ...     seq.sort(key=str)
    ...     return seq
    >>> print sorted(a.keys())
    [1, 2]
    >>> exec "x = 3; print x" in a
    3
    >>> print sorted(a.keys())
    [1, 2, 'x']
    >>> print a['x']
    3
    >>>

Now I'll show that defaultdict instances have dynamic instance variables,
just like classic classes:

    >>> a.default = -1
    >>> print a["noway"]
    -1
    >>> a.default = -1000
    >>> print a["noway"]
    -1000
    >>> 'default' in dir(a)
    True
    >>> a.x1 = 100
    >>> a.x2 = 200
    >>> print a.x1
    100
    >>> d = dir(a)
    >>> 'default' in d and 'x1' in d and 'x2' in d
    True
    >>> print sortdict(a.__dict__)
    {'default': -1000, 'x1': 100, 'x2': 200}
    >>>
 defaultdict2 N/A dict N/A N/A __slots__ N/A default __init__ N/A N/A self default N/A None N/A N/A N/A N/A dict __init__ self N/A N/A self default default N/A __getitem__ N/A N/A self key N/A N/A N/A N/A N/A N/A N/A dict __getitem__ self key N/A N/A N/A KeyError N/A N/A N/A self default N/A get N/A N/A self key N/A args N/A N/A N/A args N/A N/A args N/A N/A self default N/A N/A N/A dict get self key args N/A merge N/A N/A self other N/A N/A N/A key other N/A N/A N/A key self N/A N/A N/A self N/A key N/A other N/A key N/A N/A N/A test_2 

The __slots__ declaration takes a list of instance variables, and reserves
space for exactly these in the instance. When __slots__ is used, other
instance variables cannot be assigned to:

    >>> a = defaultdict2(default=0.0)
    >>> a[1]
    0.0
    >>> a.default = -1
    >>> a[1]
    -1
    >>> a.x1 = 1
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    AttributeError: 'defaultdict2' object has no attribute 'x1'
    >>>

 N/A test_3 

Introspecting instances of built-in types

For instance of built-in types, x.__class__ is now the same as type(x):

    >>> type([])
    <type 'list'>
    >>> [].__class__
    <type 'list'>
    >>> list
    <type 'list'>
    >>> isinstance([], list)
    True
    >>> isinstance([], dict)
    False
    >>> isinstance([], object)
    True
    >>>

Under the new proposal, the __methods__ attribute no longer exists:

    >>> [].__methods__
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    AttributeError: 'list' object has no attribute '__methods__'
    >>>

Instead, you can get the same information from the list type:

    >>> pprint.pprint(dir(list))    # like list.__dict__.keys(), but sorted
    ['__add__',
     '__class__',
     '__contains__',
     '__delattr__',
     '__delitem__',
     '__delslice__',
     '__doc__',
     '__eq__',
     '__format__',
     '__ge__',
     '__getattribute__',
     '__getitem__',
     '__getslice__',
     '__gt__',
     '__hash__',
     '__iadd__',
     '__imul__',
     '__init__',
     '__iter__',
     '__le__',
     '__len__',
     '__lt__',
     '__mul__',
     '__ne__',
     '__new__',
     '__radd__',
     '__reduce__',
     '__reduce_ex__',
     '__repr__',
     '__reversed__',
     '__rmul__',
     '__setattr__',
     '__setitem__',
     '__setslice__',
     '__str__',
     '__subclasshook__',
     'append',
     'count',
     'extend',
     'index',
     'insert',
     'pop',
     'remove',
     'reverse',
     'sort']

The new introspection API gives more information than the old one:  in
addition to the regular methods, it also shows the methods that are
normally invoked through special notations, e.g. __iadd__ (+=), __len__
(len), __ne__ (!=). You can invoke any method from this list directly:

    >>> a = ['tic', 'tac']
    >>> list.__len__(a)          # same as len(a)
    2
    >>> a.__len__()              # ditto
    2
    >>> list.append(a, 'toe')    # same as a.append('toe')
    >>> a
    ['tic', 'tac', 'toe']
    >>>

This is just like it is for user-defined classes.
 N/A test_4 

Static methods and class methods

The new introspection API makes it possible to add static methods and class
methods. Static methods are easy to describe: they behave pretty much like
static methods in C++ or Java. Here's an example:

    >>> class C:
    ...
    ...     @staticmethod
    ...     def foo(x, y):
    ...         print "staticmethod", x, y

    >>> C.foo(1, 2)
    staticmethod 1 2
    >>> c = C()
    >>> c.foo(1, 2)
    staticmethod 1 2

Class methods use a similar pattern to declare methods that receive an
implicit first argument that is the *class* for which they are invoked.

    >>> class C:
    ...     @classmethod
    ...     def foo(cls, y):
    ...         print "classmethod", cls, y

    >>> C.foo(1)
    classmethod test.test_descrtut.C 1
    >>> c = C()
    >>> c.foo(1)
    classmethod test.test_descrtut.C 1

    >>> class D(C):
    ...     pass

    >>> D.foo(1)
    classmethod test.test_descrtut.D 1
    >>> d = D()
    >>> d.foo(1)
    classmethod test.test_descrtut.D 1

This prints "classmethod __main__.D 1" both times; in other words, the
class passed as the first argument of foo() is the class involved in the
call, not the class involved in the definition of foo().

But notice this:

    >>> class E(C):
    ...     @classmethod
    ...     def foo(cls, y): # override C.foo
    ...         print "E.foo() called"
    ...         C.foo(y)

    >>> E.foo(1)
    E.foo() called
    classmethod test.test_descrtut.C 1
    >>> e = E()
    >>> e.foo(1)
    E.foo() called
    classmethod test.test_descrtut.C 1

In this example, the call to C.foo() from E.foo() will see class C as its
first argument, not class E. This is to be expected, since the call
specifies the class C. But it stresses the difference between these class
methods and methods defined in metaclasses (where an upcall to a metamethod
would pass the target class as an explicit first argument).
 N/A test_5 

Attributes defined by get/set methods


    >>> class property(object):
    ...
    ...     def __init__(self, get, set=None):
    ...         self.__get = get
    ...         self.__set = set
    ...
    ...     def __get__(self, inst, type=None):
    ...         return self.__get(inst)
    ...
    ...     def __set__(self, inst, value):
    ...         if self.__set is None:
    ...             raise AttributeError, "this attribute is read-only"
    ...         return self.__set(inst, value)

Now let's define a class with an attribute x defined by a pair of methods,
getx() and and setx():

    >>> class C(object):
    ...
    ...     def __init__(self):
    ...         self.__x = 0
    ...
    ...     def getx(self):
    ...         return self.__x
    ...
    ...     def setx(self, x):
    ...         if x < 0: x = 0
    ...         self.__x = x
    ...
    ...     x = property(getx, setx)

Here's a small demonstration:

    >>> a = C()
    >>> a.x = 10
    >>> print a.x
    10
    >>> a.x = -10
    >>> print a.x
    0
    >>>

Hmm -- property is builtin now, so let's try it that way too.

    >>> del property  # unmask the builtin
    >>> property
    <type 'property'>

    >>> class C(object):
    ...     def __init__(self):
    ...         self.__x = 0
    ...     def getx(self):
    ...         return self.__x
    ...     def setx(self, x):
    ...         if x < 0: x = 0
    ...         self.__x = x
    ...     x = property(getx, setx)


    >>> a = C()
    >>> a.x = 10
    >>> print a.x
    10
    >>> a.x = -10
    >>> print a.x
    0
    >>>
 N/A test_6 

Method resolution order

This example is implicit in the writeup.

>>> class A:    # classic class
...     def save(self):
...         print "called A.save()"
>>> class B(A):
...     pass
>>> class C(A):
...     def save(self):
...         print "called C.save()"
>>> class D(B, C):
...     pass

>>> D().save()
called A.save()

>>> class A(object):  # new class
...     def save(self):
...         print "called A.save()"
>>> class B(A):
...     pass
>>> class C(A):
...     def save(self):
...         print "called C.save()"
>>> class D(B, C):
...     pass

>>> D().save()
called C.save()
 A N/A object N/A m N/A N/A self N/A N/A N/A A N/A N/A B N/A A N/A m N/A N/A self N/A N/A N/A N/A B N/A N/A N/A super B self m N/A N/A C N/A A N/A m N/A N/A self N/A N/A N/A N/A C N/A N/A N/A super C self m N/A N/A D N/A C B N/A m N/A N/A self N/A N/A N/A N/A D N/A N/A N/A super D self m N/A N/A N/A test_7 

Cooperative methods and "super"

>>> print D().m() # "DCBA"
DCBA
 N/A test_8 

Backwards incompatibilities

>>> class A:
...     def foo(self):
...         print "called A.foo()"

>>> class B(A):
...     pass

>>> class C(A):
...     def foo(self):
...         B.foo(self)

>>> C().foo()
Traceback (most recent call last):
 ...
TypeError: unbound method foo() must be called with B instance as first argument (got C instance instead)

>>> class C(A):
...     def foo(self):
...         A.foo(self)
>>> C().foo()
called A.foo()
 N/A __test__ N/A tut1 tut2 tut3 tut4 tut5 tut6 tut7 tut8 test_1 test_2 test_3 test_4 test_5 test_6 test_7 test_8 test_main N/A N/A verbose N/A None N/A test test_support test_descrtut N/A N/A N/A test_support run_doctest test_descrtut verbose N/A N/A N/A __name__ __main__ N/A N/A N/A test_main 1