Reversing a string

A

Alex Martelli

Aahz said:
This works in all versions of Python back to 1.5.2 IIRC. reversed() is
a moderately new built-in function;

Yep: it came with Python 2.4, first alpha just 4 years ago, final
release about 3 years and 8 months ago. "Moderately new" seems an
appropriate tag.
I would agree with people who claim
that you should memorize most of the built-in functions (which is
precisely why there is a high barrier to adding more built-in functions).

I think the built-in functions and types a beginner is likely to need
are a "fuzzy subset" (the decision of whether to include or exclude
something being not really obvious:) roughly including:

abs all any bool chr cmp dict dir enumerate float getattr help hex int
iter len list max min object open ord property raw_input reversed set
sorted str sum tuple unichr unicode xrange zip

all reasonably documented at
<http://docs.python.org/lib/built-in-funcs.html> . Of course, as I
mentioned, most inclusions and exclusions may be debatable (why do I
think people need xrange and not necessarily range, set and not
necessarily frozenset, property rather than classmethod, hex more likely
than oct, probably not complex, etc etc).

But certainly if you're using a datatype you should make a point of
reading all its documentation, which would mean you'd know that list()
can convert any iterable type.

Yes, and also the methods and operators of (out of the builtin types I
listed above):

dict float int list open[*] set str tuple unicode

[*] well really file, that's the name of the builtin type, but open is
what one should use as a factory function for it!-)

str and unicode have almost identical methods, save for the big crucial
difference on the semantics of method .translate; float and int also
have the same operators; and tuple has hardly anything, so the learning
task is nowhere as big as it may seem from here:).


Alex
 
J

Jan Vorwerk

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):

---------------------------------------['__builtins__', '__doc__', '__name__']
['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']
['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__',
'__init__', '__iter__', '__len__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__str__', 'next']
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
 
M

Martin Durkin

(e-mail address removed) (Alex Martelli) wrote in

I think the built-in functions and types a beginner is likely to need
are a "fuzzy subset" (the decision of whether to include or exclude
something being not really obvious:) roughly including:

abs all any bool chr cmp dict dir enumerate float getattr help hex int
iter len list max min object open ord property raw_input reversed set
sorted str sum tuple unichr unicode xrange zip

Thanks guys, that is helpful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top