Introducing Python by example (Enrico Franchi)

annuncio pubblicitario
Introducing Python
by Example
Enrico Franchi
[email protected]
1
Outline
Very (very )* short introduction to Python
Some small examples
Evolutionary game
Parlando del
futuro...
We will perhaps eventually be
writing only small modules that are
identified by name as they are
used to build larger ones, so that
devices like indentation, rather than
delimiters, might become feasible
for expressing local structure in the
source language.
Donald E. Knuth, Structured
Programming with go to
Statements, 1974
3
Introduzione
Python è concepito da Guido van Rossum alla fine
degli anni ‘80 per Amoeba
Pubblico 1991, stabile 1994.
Linguaggio di alto livello ed orientato agli oggetti.
Utilizzato per programmazione di sistema e di rete, e
calcolo scientifico, applicazioni desktop, integrazione di
videogiochi
Si impone in ambito web/enterprise, con soluzioni
come Zope/Plone, Django,Twisted, GAE, Tornado.
4
5
READABILITY
COUNTS
Zen of Python
Oggetti in Python
In Python tutto è un oggetto:
Un numero, una stringa sono oggetti
Gli oggetti sono oggetti (ehm...)
Una funzione è un oggetto
Una classe è un oggetto
Gli oggetti sono cittadini di prima classe,
possiamo manipolarli riccamente e
comodamente (introspezione, etc.)
Possiamo fare, in definitiva, tutto
6
Tipizzazione in Python
Python è un linguaggio ad oggetti a tipizzazione
dinamica e forte
Tipizzazione forte:
Gli errori di tipo sono sempre generati. Es. Stringhe
non diventano interi e viceversa
Ogni oggetto ha una classe, questa non cambia
Tipizzazione dinamica
Gli errori di tipo sono generati a runtime
Duck typing
7
Hello, world!
print “Hello, world!”
8
Dettagli implementativi
Tipicamente Python viene
compilato a byte-code e
questo viene interpretato da
una macchina virtuale (come
Java)
Diversamente da Java la
compilazione è trasparente
per l’utente
Possiamo anche usare
l’interprete interattivo
$ cat hello.py
#!/usr/bin/python
print "Hello, world!"
$ python hello.py
Hello, world!
$ chmod 755 hello.py
$ ./hello.py
Hello, world!
$ python
Python 2.5.1 (...)
...
>>> print "Hello, world"
Hello, world
9
Interprete interattivo
L’interprete interattivo ufficiale ha
come prompt >>>
Scriviamo comandi (statements)
che vengono byte-compilati ed
eseguiti
Se il comando valuta in un
espressione (es. un expression
statement), l’espressione viene
stampata
10
>>> import os
>>> print “foo”
foo
>>> os.getcwd()
“/Users/enric/pycourse”
>>> import sys
>>> sys.stdout.write(“ciao\n”)
ciao
>>> def f(a):
...
sys.stdout.write(a)
...
return a
...
>>> f(“ciao\n”)
ciao
“ciao\n”
Esempio 01:
System Scripting
import os
import shutil
for fname in os.listdir(os.getcwd()):
if fname.endswith(('pyc', 'pyo')):
os.remove(fname)
elif fname.endswith('py'):
shutil.copy(fname, fname + '.bak')
11
Esempio 2:
“semplice wget” (GvR)
import sys
import urllib
import os
def hook(*a):
print '%s: %s' % (fn, a)
for url in sys.argv[1:]:
fn = os.path.basename(url)
print url, "->", fn
urllib.urlretrieve(url, fn, hook)
12
Evolution!
https://github.com/rik0/isle
Animals
0
1
2
7
M
3
6
5
4
Animal
+ x : int
+ y : int
+ energy : int
+ dir : int
+ genes : int[8]
+ move() : void
+ turn() : void
+ eat(plants : cell[0..]) : void
+ reproduce() : Animal[0..1]
Dir
Gene
0
1
2
3
4
5
6
7
1
1
10
1
1
1
1
1
https://github.com/rik0/isle
UI
https://github.com/rik0/isle
Scarica