========== Why Python ========== ---------------------------- *Because XKCD said so* ---------------------------- .. class:: right `Morgan Goose http://morgangoose.com` October 2010 .. class:: handout 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. Three simple reasons ==================== 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) .. _Python: http://docs.python.org Some reasons not to use Python_ =============================== 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 Why reading code is more important ================================== *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) Having your lists, and munging them too ======================================= Python out of the box has and extensively leverages their small set of basic types: list, tuple, set, dict, string, float, int, bool, etc_ .. _etc: http://docs.python.org/library/stdtypes.html 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. Documentation before all ======================== 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) .. code-block:: python """ Module doc """ def bar(): """ Function doc """ pass We're all devs here, and we're all lazy ======================================= 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. .. _Fabric: http://docs.fabfile.org .. _nose: http://somethingaboutorange.com/mrl/projects/nose/0.11.2/ .. _Sphinx: http://sphinx.pocoo.org/ .. _pip: http://pypi.python.org/pypi/pip .. _virtualenv: http://pypi.python.org/pypi/virtualenv .. _Mercurial: http://mercurial.selenic.com/ .. _bpython: http://bpython-interpreter.org/ .. _bottle: http://bottle.paws.de/docs/dev/index.html .. _flask: http://flask.pocoo.org/ .. _sqlalchemy: http://www.sqlalchemy.org/ .. _elixir: http://elixir.ematia.de/trac/wiki It's like a language concentrate ================================ Python is dense. You can leverage a lot of awesome things in relatively few lines, without managing to become obfuscated: .. code-block:: python >> 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'] You get to run with scissors ============================ Python will let you have a function that doesn't know what some parameters are until run-time, as well as return multiple values: .. code-block:: python 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] Other fun bits being ==================== * 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