[Python-checkins] CVS: python/nondist/peps pep-0252.txt,1.12,1.13

Guido van Rossum gvanrossum@users.sourceforge.net
2001年7月13日 14:50:50 -0700


Update of /cvsroot/python/python/nondist/peps
In directory usw-pr-cvs1:/tmp/cvs-serv23270
Modified Files:
	pep-0252.txt 
Log Message:
Added a small example before I have to go.
Index: pep-0252.txt
===================================================================
RCS file: /cvsroot/python/python/nondist/peps/pep-0252.txt,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -r1.12 -r1.13
*** pep-0252.txt	2001年07月13日 21:04:00	1.12
--- pep-0252.txt	2001年07月13日 21:50:48	1.13
***************
*** 596,600 ****
 Examples
 
! XXX
 
 
--- 596,648 ----
 Examples
 
! 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.
 
 

AltStyle によって変換されたページ (->オリジナル) /