views:

1142

answers:

13

I'm in the process of learning Python while implementing build scripts and such. And for the moment everything is working fine in that the scripts do what they need to do. But I keep having the feeling I'm missing something, such as "The Python Way". I know build scripts and glue scripts are not really the most exciting development work and may hardly be a candidate for revealing the true power of Python but I'd still like the opportunity to have my mind blown. I develop mostly in C# and I find that my Python code looks awfully similar in structure and style to a lot of my C# code. In other words I feel like I'm thinking in C# but writing in Python.

Am I really missing something?

(Note: I realize this isn't so much a programming question and it's quite broad and there may not be a definitive answer so mod me down into oblivion if you have to.)

+15  A: 

No - this is common for folks who move to Python from other C-like languages. I believe what you are looking for is ways to make your code more "Pythonic". The good news is the more Python you write the more Pythonic your code will become. It is a natural overflow of the "how can I do this more simply" attitude.

Another good place to look at is The Zen of Python. These attitudes towards Python development will also help you in the same regard.

Andrew Hare
Thanks for the "Pythonic" link. It's given me a better idea of what to shoot for now.
Jonathon Watney
+27  A: 

I would recommend that you read up on Generators, Iterators, itertools and above all List Comprehensions.

These are the pillars of anything Pythonic. And for everything else, there is PEP-8.

Read up on these concepts and try using them wherever appropriate. All the best!

PS: Don't forget to import this ;)

Addendum: I would also aggregate some excellent suggestions given by others in this thread here:

Baishampayan Ghose
The combination of generators and list comprehensions looks fantastic. I think I'll using more of those. Thanks. :)
Jonathon Watney
itertools is a great module for doing things really elegantly. its often forgotten about.
Richard Levasseur
Don't forget, python was already a highly useful language before the existence of generators, iterators, itertools, list comprehensions, or even PEPS!
Greg Ball
+3  A: 

To add to the answers of Andrew Hare and Baishampayan Ghose...

To learn the idiom of any language must involve reading code written in that idiom. I'm still learning the Python idiom, but I've been through this with other languages. I can read about list comprehensions, but the lightbulb only really comes on when you see such things in use and say, "Wow! That's awesome! Two lines of code and it's crystal clear!" So go find some pythonic code that you find interesting and start reading it and understanding it. The knowledge will stay in your head better if you see everything in the context of a working program.

dwc
+4  A: 

Recently I've been learning/improving my python by solving the Project Euler problems in python. This has worked really well for me because:

  1. It is fun and competitive, so I'm motivated to keep going
  2. It forces me to use the python data structures in a really natural way to get the performance I need, so has taught me a lot about lists, sets, strings, iteration etc.
  3. Most of the problems need less than a page of code to solve, so you have more time to think about polishing or rewriting in a more elegant way
  4. Python copes with large integers really easily, and so it just feels like the right language to use

I'd thoroughly recommend this.

Nick Fortescue
I've also learned a lot doing some of the Project Euler problems in Python. Especially the functional programming aspects of Python using the builtins and the itertools module.
Kimmo Puputti
+5  A: 

You've gotten good suggestions so far. I'd only add Dive Into Python.

jlc
+1  A: 

Another powerhouse resource: Code Like a Pythonista: Idiomatic Python.

David Grant
+1  A: 

While build scripts and clue scripts [sic] are not really the most exciting development work, they are completely a candidate for revealing the true power of Python.

First, look at the various make-like tools already written in Python.

The big fish in this pond is Scons: http://www.scons.org/. Learn about this.

This blog post talks about the alternatives http://farmdev.com/thoughts/46/the-python-make-tool/

"I'd still like the opportunity to have my mind blown" Start with Google: find Python tools that already do some or all of what you're trying to do. Code less, download and read more.

S.Lott
+5  A: 

Are you reading Python you haven't written?

Here's a script from the Python 2.6.1 distribution that deletes .pyc and .pyo files.

#!/usr/local/bin/python
"""Recursively zap all .pyc and .pyo files"""
import os
import sys

# set doit true to actually delete files
# set doit false to just print what would be deleted
doit = 1

def main():
    if not sys.argv[1:]:
        if os.name == 'mac':
            import EasyDialogs
            dir = EasyDialogs.AskFolder(message='Directory to zap pyc files in')
            if not dir:
                sys.exit(0)
            zappyc(dir)
        else:
            print 'Usage: zappyc dir ...'
            sys.exit(1)
    for dir in sys.argv[1:]:
        zappyc(dir)

def zappyc(dir):
    os.path.walk(dir, walker, None)

def walker(dummy, top, names):
    for name in names:
        if name[-4:] in ('.pyc', '.pyo'):
            path = os.path.join(top, name)
            print 'Zapping', path
            if doit:
                os.unlink(path)

if __name__ == '__main__':
    main()

How many Python idioms can you find in that?

Thomas L Holaday
Four. Of course, I spot a couple of anti-idioms, too. ;-)
Ben Blank
+5  A: 

You should definitely take a look at this talk, when you start doing systems programming with python: http://www.dabeaz.com/generators/

André
Oh wow, that's some cool stuff those generators!
Jonathon Watney
A: 

I would suggest finding a personal python guru. Show them some of your code and have them review/rewrite it into idiomatic python. Thus will you be enlightened.

Greg Ball
A: 

To echo TLHOLADAY, read the standard library. That's where the "pythonic" stuff is. If you're not getting a good feel there, then read the source for sqlachemy or django or your project of choice.

Gregg Lind
+2  A: 

Write some Python code and post it on SO for review and feedback whether it is pythonic.

Lakshman Prasad