Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Am I missing something? [closed]

Tags:

python

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

like image 651
Jonathon Watney Avatar asked Feb 19 '09 19:02

Jonathon Watney


People also ask

How do you fix not closed in Python?

Replacing "content" = simple_storage_file with "content": simple_storage_file should solve your issue.

What happens if you dont close file Python?

If you write to a file without closing, the data won't make it to the target file. But after some surfing I got to know that Python automatically closes a file when the reference object of a file is reassigned to another file. It is a good practice to use the close() method to close a file.

What is close () in Python?

Python File close() Method The close() method closes an open file. You should always close your files, in some cases, due to buffering, changes made to a file may not show until you close the file.

Do Python files need to be closed?

You've learned why it's important to close files in Python. Because files are limited resources managed by the operating system, making sure files are closed after use will protect against hard-to-debug issues like running out of file handles or experiencing corrupted data.


8 Answers

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:

  • Read Code Like a Pythonista: Idiomatic Python (from David Grant)
  • Solving problems from Project Euler is a great idea (from Nick Fortescue)
  • This excellent presentation on Generators and Iterators by David Beazley (from André)
  • Learn about Scons (from the venerable S.Lott)
like image 97
Baishampayan Ghose Avatar answered Oct 04 '22 01:10

Baishampayan Ghose


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.

like image 28
Andrew Hare Avatar answered Oct 04 '22 01:10

Andrew Hare


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

EDIT: As of Oct 4, 2011, this work can be found here. Dive Into Python 3, here. "Why" this is: see here, here and here.

like image 26
jlc Avatar answered Oct 04 '22 01:10

jlc


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

like image 20
André Avatar answered Oct 04 '22 01:10

André


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.

like image 24
Nick Fortescue Avatar answered Oct 04 '22 00:10

Nick Fortescue


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?

like image 42
Thomas L Holaday Avatar answered Oct 04 '22 00:10

Thomas L Holaday


Think like this:

  • If you are writing too much for little work, something is wrong, this is not pythonic.

Most Python code you will write is very simple and direct. Usually you don't need much work for anything simple. If you are writing too much, stop and think if there is a better way. (and this is how I learned many things in Python!)

like image 28
Denilson Sá Maia Avatar answered Oct 04 '22 01:10

Denilson Sá Maia


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.

like image 25
dwc Avatar answered Oct 04 '22 00:10

dwc