Mastering Python... Best Resources?

Travis Parks jehugaleahsa at gmail.com
Fri Aug 26 17:36:54 CEST 2011


On Aug 26, 11:12 am, Roy Smith <r... at panix.com> wrote:
> In article
> <2309ec4b-e9a3-4330-9983-1c621ac16... at ea4g2000vbb.googlegroups.com>,
>  Travis Parks <jehugalea... at gmail.com> wrote:
>
> > I know the Python syntax pretty well. I know a lot of the libraries
> > and tools. When I see professional Python programmer's code, I am
> > often blown away with the code. I realized that even though I know the
> > language, I know nothing about using it effectively.
>
> In a sense, I'm in the same boat as you.  I've been using Python since
> before the 2.0 series, and I tend to think of the language in much the
> same way as I did back then.  Which is to say I don't use the language,
> as it currently exists, as effectively as I might.
>
> Here's some things I suggest you look at:
>
> Iterators.  This is such a powerful concept.  When I started with the
> language, iterators largely meant the difference between range() and
> xrange().  Now we've got a whole ecosystem which has grown up around
> them (x + " comprehension" for x in {'list', 'dictionary', 'set'}), not
> to mention generators and generator expressions.  And the itertools
> library.
>
> Decorators.  Another powerful concept.  We use these in our web servers
> for all sorts of cool things.  Adding cacheing.  Imposing prerequisites
> on route calls.  I still don't think of using these immediately, but I
> do see the notational convenience they provide for many things.
>
> Context Managers.  One of the (very few) things that I always found
> lacking in Python compared to C++ was deterministic object destruction.  
> Context managers give you this.  I'm still exploring all the neat things
> you can do with them.
>
> The full range of containers.  I started with lists, tuples, and
> dictionaries.  Now we've got sets, frozensets, named tuples, deques,
> Counters, defaultdicts (I love those), heaps, and I'm sure a few others
> I've missed.  List and dicts are such well designed containers, you can
> do almost anything with just those two, but all the other new ones often
> make things quicker, simpler, and more obvious.
>
> The profiler.  Most people obsess about performance early on and don't
> realize that most of their guesses about what's fast and what's slow are
> probably wrong.  Learn to use the profiler and understand what it's
> telling you.
>
> Unittest.  Testing is, in general, a neglected practice in most software
> development shops, and that's a shame.  Python has some really good
> capabilities to support testing which you should get familiar with.  
> Unittest is just one of them.  There's also doctest, nose, and a bunch
> of other contributed modules.  Look at them all, learn at least one of
> them well, and use it for everything you write.
>
> > I've read quite a few books about Python. They cover a lot of topics,
> > but none of them covered common conventions or hacks. I mean, I got
> > good at C++ reading books by Scott Meyers, who concentrated on common
> > idioms, things to avoid, the proper way to do things, etc.
>
> Ugh.  The problem with Meyers's books is that they are needed in the
> first place.  C++ is such a horribly complicated language, you really
> can't use it without making a serious study of it.  There's too many
> gotchas that you MUST know to avoid disaster with even the most basic
> programs.
>
> Python isn't that way.  You can learn a small, basic subset of the
> language and get a lot done.  You may not be doing things the most
> effective way, but you're also not going to be looking at memory
> corruption because you didn't understand the details of object lifetimes
> or how type promotion, function overloading, and implicit temporary
> object construction all interact.
>
>

Thanks for the input.

I had been writing my Compass project (http://compass.codeplex.com) in
Pythonese. I was planning on implementing a lot of the features of MS'
LINQ in Python iterators, too. I am surprised that there aren't a ton
of Python libraries for general purpose algorithms. "yield" is one of
my favorite keywords. :-)

I will take a look at decorators especially. I see them being used for
properties and other coolness. I started playing with unittest the
other day.

unittest.main(exit=False) <-- took me a while to find

I will look at the containers, too. I have been trying to push tuple
syntax support in C# for years now. Named tuples are so useful.

I agree that C++ is too complicated. Bjarne should have cared less
about backward compatibility with C and fixed some of the issues with
it. He should have also made some of the defaults more intuitive -
like ctor initializers being reorganized to the same order as the
backing fields... that'll getcha. Function argument order evaluation.
Oh no! It might run .00001 seconds slower on a Sparc machine! I think
anal programmers like me gravitate towards C++ because we like to show
how smart we are by naming off some arbitrary fact. Sure, it doesn't
make development any better, but it gives us something to waste memory
with. I find myself getting super excited when I get to use words like
'readonly' in C# (Mwahahah! I used a keyword no one else uses!). What
drives me nuts is that Bjarne defends his language to the death. It is
nice to see Guido trying to fix things in Py3.



More information about the Python-list mailing list