python for everyday tasks

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Nov 22 21:01:26 EST 2013


On Fri, 22 Nov 2013 15:59:19 -0800, koch.mate wrote:

> Hello,
> 
> I'm about held a short course with the title indicated in the subjects.
> The students are very experienced programmers of our company, with deep
> knoledge on C, C++, C#, Perl and similar languages, but very limited, or
> absolutely no knowledge on python.
> 
> what would you teach to such a group in 5x1.5 hours? I'm looking for the
> most interesting, unique topics, emphesizing python's strong points.
> 
> I have already a couple ideas:
>  - a general intro about tuples, lists, dicts, sets, and working with
>  these - functional programming tools, functools, itertools, lambda,
>  map, filter - wsgi, pickle


Do you have any system administrators in the audience? If so, I'd show 
the IPython shell, which includes a lot of custom "magic" to make it a 
powerful shell as easy to use as bash. For example, compare the regular 
Python REPL:

py> import os, glob
py> glob.glob(os.path.expanduser('~/lam*'))
['/home/steve/lambert.ods', '/home/steve/lambert.csv', '/home/steve/
lambertw.pdf', '/home/steve/lambert-v2.pdf', '/home/steve/lambert.txt']


with the IPython shell:

In [1]: ls ~/lam*
/home/steve/lambert.csv  /home/steve/lambert.txt     /home/steve/
lambertw.pdf
/home/steve/lambert.ods  /home/steve/lambert-v2.pdf


You'll probably want to discuss the similarities and differences between 
Python and other languages. In my experience, the most confusing part of 
learning a new language is the things which are *almost* but not quite 
the same, rather than those which are radically different. When things 
are obviously different, you come in to it with no preconceived ideas.


* Python uses name binding, not variables at fixed locations;

* therefore there are no pointers (except under the hood, in the 
implementation);

* but you won't miss them. Honest.

* Python uses "call by object sharing", which is exactly the same calling 
convention that the Java people call "call by value" and Ruby people call 
"call by reference". Here's an opinionated post I wrote some years ago 
describing the differences:

https://mail.python.org/pipermail/tutor/2010-December/080505.html


* Python is not Java, and Java is not Python either:

http://dirtsimple.org/2004/12/python-is-not-java.html
http://dirtsimple.org/2004/12/java-is-not-python-either.html

* Everything in Python is an object. *Everything*. There is no 
distinction between boxed and unboxed integers, for example, they're all 
boxed.

* When I say everything, I mean it. Functions and methods are objects 
too. So are classes, and yes, that means that you can inspect the class 
of a class (the metaclass).

* Need high performance numeric computing? Python tools like numpy, 
pandas, nltk and others are rapidly becoming *the* standard tool for 
numeric and scientific computing:

http://blog.mikiobraun.de/2013/11/how-python-became-the-language-of-
choice-for-data-science.html

http://www.talyarkoni.org/blog/2013/11/18/the-homogenization-of-
scientific-computing-or-why-python-is-steadily-eating-other-languages-
lunch/


* Some terminology differences:

  - What some languages call members, or class variables and 
    instance variables, Python usually calls class or instance 
    attributes. (Although the documentation is a tad inconsistent
    in this.) This makes sense: if a string variable is a 
    variable holding a string, and an int variable is a variable
    holding an int, then surely a class variable is a variable 
    holding a class. (Remember what I said about classes being 
    objects too?)

  - Python class methods are what Java calls static methods, and
    Python static methods are kind of like functions only not.
    (You'll probably never find a good use for static methods in 
    Python.)

  - There are no procedures or void functions in Python, but 
    there are functions which return None, which is as close as
    you'll get to nil or null.

  - I already mentioned the pass by object sharing thing.

* The Zen of Python: at the interactive interpreter, type "import this". 
It's not quite a joke and not quite serious, but a little of both, and it 
gives a good insight into what the Python core developers consider best 
practice.

* Note to Perl coders: you'll notice that "Only One Way To Do It" is NOT 
in there, and it never has been.

* Docstrings and doctest.

* Unittest is great for test-driven development.

* Python 3 (although not Python 2) is one of the few languages that get 
Unicode *right*. Strings in Python 3 are text, sequences of Unicode 
characters, not a thinly disguised blob of bytes. Starting with Python 
3.3, Python does away with the difference between "narrow builds" (which 
save memory at the expense of correctness) and "wide builds" (which give 
correct Unicode behaviour at the cost of memory). Instead, Python 3.3 now 
has optimized strings that use only as much memory as needed. Pure ASCII 
strings will use 1 byte per character, while Unicode strings use 1, 2 or 
4 bytes per character as needed. And it all happens transparently.

* If you want a blob of bytes, Python 3 has the bytes and bytearray 
classes. But don't use blobs of bytes when you want text.

* Python comes standard with support for the main Unicode encodings 
(UTF-8, UTF-16, UTF-32 and UTF-7), plus a whole mess of legacy encodings, 
like ASCII, Latin-1, ISO-8859-7, and many more.

* In Python, at least the standard CPython implementation, threads won't 
give you any benefit in CPU-bound tasks, although they will speed up IO-
bound tasks. On Linux, at least, using multiple processes instead of 
multiple threads is the way to go.

* List comprehensions (borrowed from Haskell, but with nicer syntax).

* Lazy processing with generators and generator expressions.

* Coroutines: http://dabeaz.com/coroutines/



Some of these things (e.g. coroutines, metaclasses) can be extremely 
advanced, and perhaps you only want to mention them in passing just to 
give a flavour for what you can do with Python. 


-- 
Steven



More information about the Python-list mailing list