I'm a python addict !

Paul McGuire ptmcg at austin.rr.com
Fri Jan 23 23:25:42 EST 2009


On Jan 23, 8:58 pm, Linuxguy123 <linuxguy... at gmail.com> wrote:
> I just started using python last week and I'm addicted.
>     <snip youthful exuberance>
> I see a lot of Python in my future.

Bravo, welcome, and well-said!

Your sentiments mirror and resonate with those of many on this list.
About 6 years ago I wanted a scripting language to work with,
*anything* but more Tcl/Tk!  Python the language had many features and
language design decisions that just "felt right," and the natural feel
of the object model allowed me to leverage much of the OO theory I had
learned using Java and C++.

And THEN, it began to dawn on me that a lot of that theory is actually
crutch, bailing-wire, and gum, put there to work around the static
typing rigidity of C++, Java and their ilk.  As much as I loved Scott
Meyers' "Effective C++" books, many of their idioms and tricks just
fall away as unnecessary in Python.  Interfaces?  Python don't need no
steenking interfaces!  The interface concept is still useful for
organizing your thinking and OO design, but the implementation is far
freer, as you find that an object's conformance to an interface is
only determined/required at runtime, not at some long-distant compile
step in a library nested N levels deep in your hierarchy.

Python exposes many of its dynamic hooks through magic __xxx__
methods.  Need to write a proxy wrapper around an object of type X to
supercede method Y, but you don't want to write all thouse tedious
passthru methods for the X class's methods A thru R?  Just implement
your special method Y, and let a __getattr__ method pass all the other
calls on to the underlying object.  Instant Proxy!

Want to change the type/behavior of an object from class A to class
B?  How about this:

    aobj = A()
    aobj.__class__ = B

Try *that* in as simple-looking C++ or Java!

Python's learning curve can be gentle or steep, depending on your own
inclination.  My advice?  Push yourself up as quickly as you can.
Quickly put aside this kind of code:

    for i in range(len(datalist)):
        # do something with datalist[i]

and start thinking in iterators:

    for item in datalist:
        # do something with item

Need to read through a text file?

    for line in file("xyzzy.dat"):
        # do something with each line

Master the clean look and feel of a list comprehension or generator
expression, over explicit for loops.  Need to build a list of the odd
numbers up to 100?  Instead of this:

    oddnums = []
    for n in range(100):
        if n % 2:
            oddnums.append(n)

learn to do this:

    oddnums = [ n for n in range(100) if n % 2 ]

Learn the native data structures - tuple, list, dict, set.  Learn the
dict idiom that serves as Pythons version of a switch statement.
Check out the itertools module, a cunning mix of elegance and
wizardry.  The standard lib is a trove of well-oiled and production-
tested code, but the community is not shy about producing additional
modules, many with liberal licensing - image processing, text
processing/parsing, discrete-event simulation, optimization, genetic
algorithms, web/network apps and IPC, ... Google and ye shall find.

Write a generator method to yield a sequence of useful values (odd
numbers, primes, consonants, fibonacci's, whatever).  You can
certainly write a lot of Python without ever using these intermediate/
advanced features, but one day you'll give it a shot and think "Dang!
what was the big deal?  I wish I'd tried this sooner!"

Yet having said that, in 6 years I've not yet tried to write a C
extension for Python.  But I'm told that this too is a well-blazed
trail if you feel the need.

Dig in! And post back here if you get stuck.  Be confident that Python
has been employed in a wide range of applications and application
domains, and that if a particular feature seems to be missing, it may
be because Python takes a slightly different angle on the problem.

Ah, those heady days of youth, when the world was fresh, and the
idioms of Python were still new and ripe to discover!

I envy you, Linuxguy123, I envy you...

Enjoy!
-- Paul



More information about the Python-list mailing list