Morgan Goose http://morgangoose.com
October 2010
This work is licensed under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
If you need to have a reason to use Python over some other language that you have at your disposal, I can think of at least 3 general reasons:
- Puts the source reader first, and has a Zen code.
- Great built-ins and defaults
- Documentation. It's built into the language, and leveraged everywhere
- Ecosystem
- Everything you can do, I can do better (mostly)
While I'm liable to say and believe that this language is perfect, it's not. That's just how things are, but here are some blemishes:
- Threading (for non io bound)
- Resource usage
- A need for Pure Functional mechanisms
You read more than you write
So having a style guide for a language, that is the gold standard, means code is code is code, and everyone starts off on the same page.
There is even a few handy checking tools like pep8 or pylint to see how well your code stacks up to the style guide. (similar to the anal retentiveness needed for a w3c validation badge)
Python out of the box has and extensively leverages their small set of basic types: list, tuple, set, dict, string, float, int, bool, etc
Having these types built into the language, makes them ubiquitous, and powerful. No more making your own, or relying on third part libs to get a decent string. Everything is well defined, and has useful mechanisms attached to them.
Python docstrings are a great example of a language leveraging it's design to promote developers to document their code, and allow for users of software to simply access said documentation. (sometimes in manpage-esque fashion)
"""
Module doc
"""
def bar():
"""
Function doc
"""
pass
So we make good tools, and share them, to make more good tools:
- Fabric - ssh wrapped in great python functions
- nose - extensible unit test suite
- Sphinx - rst based documentation generator (docs.python.org)
- pip - python package manager (think cpan/gem)
- virtualenv - path monger to make simple chroot like envs
- Mercurial - pure c/python dvcs
- bpython - best atm python cli interpreter/tool
- bottle/flask - two similar micro web frameworks
- sqlalchemy/elixir - makes database backends decoupled.
Python is dense. You can leverage a lot of awesome things in relatively few lines, without managing to become obfuscated:
>> a, b = b, a #swap a and b
>> c, d, e = range(10) # c,d == 0,1 and e == range(10)[2:]
>> ["%04d" %x for x in range(5)]
['0000', '0001', '0002', '0003', '0004']
Python will let you have a function that doesn't know what some parameters are until run-time, as well as return multiple values:
def foo(a, b=None, *args, **kwargs):
if b:
print a, b
for arg in args:
print args
for k, v in kwargs.items():
print k, v, kwargs[k]
return b, a, args[0]
- decorators - functions that operate on functions to modify behavior
- generators - tool for creating iterators
- scope with with - allows for with open("file.txt") as f:
- dict comprehensions - make dictionaries on the fly
- lambdas - anonymous functions
- user defined exceptions - make your own errors