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.)
Replacing "content" = simple_storage_file with "content": simple_storage_file should solve your issue.
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.
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.
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.
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:
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.
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.
You should definitely take a look at this talk, when you start doing systems programming with python: http://www.dabeaz.com/generators/
Recently I've been learning/improving my python by solving the Project Euler problems in python. This has worked really well for me because:
I'd thoroughly recommend this.
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?
Think like this:
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!)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With