Reversing a string

Jan Vorwerk jan.vorwerk at cretin.fr
Wed Jul 4 17:46:02 EDT 2007


Martin Durkin a écrit , le 02.07.2007 06:38:
> This is an interesting point to me. I am just learning Python and I 
> wonder how I would know that a built in function already exists? 
> At what point do I stop searching for a ready made solution to a 
> particular problem and start programming my own function?
> Is it just a matter of reading *all* the documentation before I start 
> coding?

The answer from another beginner like me is: dir()

Try it out interactively (my example with Python 2.4):

---------------------------------------
 >>> dir()
['__builtins__', '__doc__', '__name__']

 >>> __builtins__
<module '__builtin__' (built-in)>

 >>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', 
'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 
'Exception', 'False', 'FloatingPointError', 'FutureWarning', 'IOError', 
'ImportError', 'IndentationError', 'IndexError', 'KeyError', 
'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 
'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 
'OverflowWarning', 'PendingDeprecationWarning', 'ReferenceError', 
'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 
'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 
'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 
'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 
'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '_', 
'__debug__', '__doc__', '__import__', '__name__', 'abs', 'apply', 
'basestring', 'bool', 'buffer', 'callable', 'chr', 'classmethod', 'cmp', 
'coerce', 'compile', 'complex', 'copyright', 'credits', 'delattr', 
'dict', 'dir', 'divmod', 'enumerate', 'eval', 'execfile', 'exit', 
'file', 'filter', 'float', 'frozenset', 'getattr', 'globals', 'hasattr', 
'hash', 'help', 'hex', 'id', 'input', 'int', 'intern', 'isinstance', 
'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'long', 'map', 
'max', 'min', 'object', 'oct', 'open', 'ord', 'pow', 'property', 'quit', 
'range', 'raw_input', 'reduce', 'reload', 'repr', 'reversed', 'round', 
'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 
'super', 'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip']

 >>> reversed
<type 'reversed'>

 >>> dir(reversed)
['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', 
'__init__', '__iter__', '__len__', '__new__', '__reduce__', 
'__reduce_ex__', '__repr__', '__setattr__', '__str__', 'next']

 >>> print reversed.__doc__
reversed(sequence) -> reverse iterator over values of the sequence

Return a reverse iterator

---------------------------------------
Far from me the idea that manuals are useless, but this is a nice thing 
about Python that you can look at what is available interactively.

Cheers
Jan



More information about the Python-list mailing list