Added a small example before I have to go.
This commit is contained in:
parent
c22397f038
commit
cfe60dc128
50
pep-0252.txt
50
pep-0252.txt
|
@ -595,7 +595,55 @@ Discussion
|
|||
|
||||
Examples
|
||||
|
||||
XXX
|
||||
Let's look at lists. In classic Python, the method names of
|
||||
lists were available as the __methods__ attribute of list objects:
|
||||
|
||||
>>> [].__methods__
|
||||
['append', 'count', 'extend', 'index', 'insert', 'pop',
|
||||
'remove', 'reverse', 'sort']
|
||||
>>>
|
||||
|
||||
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:
|
||||
|
||||
>>> T = [].__class__
|
||||
>>> T
|
||||
<type 'list'>
|
||||
>>> dir(T) # like T.__dict__.keys(), but sorted
|
||||
['__add__', '__class__', '__contains__', '__eq__', '__ge__',
|
||||
'__getattr__', '__getitem__', '__getslice__', '__gt__',
|
||||
'__iadd__', '__imul__', '__init__', '__le__', '__len__',
|
||||
'__lt__', '__mul__', '__ne__', '__new__', '__radd__',
|
||||
'__repr__', '__rmul__', '__setitem__', '__setslice__', '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']
|
||||
>>> T.__len__(a) # same as len(a)
|
||||
2
|
||||
>>> T.append(a, 'toe') # same as a.append('toe')
|
||||
>>> a
|
||||
['tic', 'tac', 'toe']
|
||||
>>>
|
||||
|
||||
This is just like it is for user-defined classes.
|
||||
|
||||
Notice a familiar yet surprising name in the list: __init__. This
|
||||
is the domain of PEP 253.
|
||||
|
||||
|
||||
Backwards compatibility
|
||||
|
|
Loading…
Reference in New Issue