New to python

Mel Wilson mwilson at the-wire.com
Tue Oct 22 09:47:24 EDT 2002


In article <B9DA8790.27E15oss at objectmentor.com>,
Bob Koss <koss at objectmentor.com> wrote:
>I'm a very experienced C++/Java programmer. Can anyone recommend papers or
>books that will help me avoid programming C++ using Python syntax and to
>learn to do things the "Python way".

   I dunno, I found the docs supplied with the Python
release to be very, very good.  I ditched the O'Reilly books
(for the first time in my life) and just learned from the
docs.

   I only know a little C++ and Java, but it seemed that
there was a pretty direct mapping of the useful parts.

   Lose the braces, as you know them, and most of the
semicolons, obviously.

   Where you would use `<vector T>`, use lists, or tuples ,,
that is [] or () . Where you would use `<map T1, T2>`, use
dictionaries ,, that is {} .

   The semantics of iterators is available, but most of the
syntax goes away.  `for item in alist:` iterates over all
the items in alist, one by one .. where `alist` is a
sequence, i.e. a list, tuple, or string.  To iterate over a
sublist, use slices:  `for item in alist[1:-1]:` does as
above, but omits the first and last items.

   For trickier iterations, read and re-read the Library doc
on the topic of general-purpose functions.  There are some
functions that apply to sequences:  map, filter, reduce,
zip. that can work wonders.  Hidden somewhere under the
documentation for sequences there is a description of string
methods that you'll want to read.

   Hidden under the docs for 'Other Types' are the
descriptions of all the file methods.  There are no
iostreams per se, but the class method __str__ can get some
of the effect for your own classes, and there are surely
other angles I haven't thought of.

   Forget polymorphism.  You can define a function, and call
it with anything you want, but if it has to behave
differently for different type operands, you have to use the
run-time type identification `type` function explicitely
within the single definition of the function.  Default
arguments to functions are just as powerful a tool as in
C++.

   In class definitions the equivalents of operator methods
are covered in a chapter in the Python Language Reference.
(Look for the double-underscore methods like __cmp__,
__str__, __add__, etc.)

   In C, the gotcha for new users is probably about
pointers; they're tricky and they can't be avoided.  The
gotchas in Python are situations when you use different
references to a single object, thinking you are using
different objects.  I believe the difference between mutable
and immutable objects comes into play.  I have no clear
answers here .. I still get caught once in a while .. keep
your eyes open.

   Read the Tutorial once, skim the Library Reference .. at
least the table of contents, then skim the Language
Reference and you will probably have encountered everything
you need.

        Regards.        Mel.



More information about the Python-list mailing list