Python – Weniger ist Mehr
oder Mehr mit Weniger
Python Geek Night, Zürich, 16. November 2010
Autor: Dr.-Ing. Mike Müller
E-Mail: mmueller@python-academy.de
Entstehung
• 1989/1990 Guido van
Rossum
• Mittelweg zwischen C und
Shell-Scripting
• Ideen von ABC, C, Smalltalk,
Java, (Haskell)
• Langsame Entwicklung in
den 1990ern
• 1999 Version 1.5.2
• Python Software Foundation
Open Source
• Python-Lizenz
• Python Software Foundation
• Kern-Entwickler
• Bibliotheks-Entwickler
• Benevolent Diktator for Life - BDFL
• Python Enhancement Proposals
Hauptmerkmale
• Einfach
• Konsistent
• Lesbar
Anwender
• Open Source
• Google (Kern-Entwickler)
• YouTube
• Deutsche Luft- und
Raumfahrt
• NASA
• Walt Disney
• Rackspace
• ...
Anwender
• Programmieranfänger
• Softwareentwickler
• Tester
• Systemadminstratoren
• Wissenschaftler und Ingenieure
• Python skaliert
• leichter Einstieg
• viele fortgeschrittene Möglichkeiten
(Meta-Programmierung)
Popularität
Popularität
• relativ leicht erlernbar
• MIT nutzt Python für Computer Science 101
• 1. Semester Python, 2. Semester C++ == 2 Semester
C++
• eingebettet in Blender, Open Office, Inkscape, Gimp
• macht vielen Leuten einfach Spaß
Betriebssysteme
• Windows
• Linux
• Mac
• Main-Frames
• Mobile Geräte
• DOS
Technologie
• Interpreter
• Plattformunabhängiger Bytecode
• Fast alles zur Laufzeit
• Einfache Meta-Programmierung
Implementierungen
• CPython == Standard Python
• Jython in Java
• IronPython in C#
• PyPy in Python
• Stackless
• Mehr
Python ist keine
Insel
• Erweitern in C/C++, Java, C#
• SWIG, SIP, Boost.Python
• .NET, COM
• Cython
• Einbetten
• Zugriff auf DLLS / Shared
Libraries
Kleben
• Glue Language
• Verbinden von (heterogen Systemen)
• Generieren von Eingabe-Daten
• Starten und Überwachen von Prozessen
• Lesen von Ausgabe-Daten
• Kommunikation über das Netzwerk
• ...
• schnell mit wenigen Code-Zeilen umsetztbar
Bibliotheken
• Reichhaltige Standardbibliothek
• Python Package Index ca. 12.000 Pakete
• Wrapper für GUI-Toolkits (wxPython, PyQt, Tkinter etc.)
• Web-Frameworks (Django, Zope, TurboGears, Pylons
etc.)
• Zugriff auf alle gängigen Datenbanken
• Wissenschaftliche Bibliotheken (NumPy, SciPy,
PyTables etc.)
Paradigmen
• Prozedural
• Objekt-orientiert
• Funktional
Syntax
• Einrückungen zählen
• Compiler "sieht" Quelltext genauso wie Programmierer
• Einrückung nach Doppelpunkt
• Ausrückung zeigt Endes eines Blockes an
• Ausführbarer Pseudocode
• Elegant
Interaktiver Modus
>>> 1 + 1
2
>>> def add(x, y):
... return x + y
...
>>> add(12, 14)
26
Scripte
# add.py
def add(x, y):
return x + y
print add(12, 14)
Ausgabe:
26
Datenstrukturen
• Eingebaut
• Fester Bestandteil der Sprache
• Listen
• Dictionarys
Listen
>>> my_list = [10, 20, 30, 40, 50]
>>> my_list[0]
10
>>> my_list[1]
20
>>> my_list[-1]
50
>>> my_list[1:3]
[20, 30]
>>> my_list[::2]
[10, 30, 50]
Listen
>>> my_list.append(60)
>>> my_list
[10, 20, 30, 40, 50, 60]
>>> my_list.append([1, 2, 3])
>>> my_list
[10, 20, 30, 40, 50, 60, [1, 2, 3]]
>>> my_list[-1] = 70
>>> my_list
[10, 20, 30, 40, 50, 60, 70]
List Comprehension
• von Haskell abgeschaut
>>> [item * 2 for item in my_list]
[20, 40, 60, 80, 100, 120, 140]
>>> [item * 2 for item in my_list if item > 40]
[100, 120, 140]
>>>
Dictionarys
• Hash-Tabellen
• Assoziative Arrays
>>> my_dict = {'a': 100, 'b': 200, 'c': 300}
>>> my_dict
{'a': 100, 'c': 300, 'b': 200}
>>> my_dict['a']
100
Dictionarys
>>> my_dict['x']
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
KeyError: 'x'
>>> my_dict['x'] = 3
>>> my_dict
{'a': 100, 'x': 3, 'c': 300, 'b': 200}
>>> my_dict['a'] = -1
>>> my_dict
{'a': -1, 'x': 3, 'c': 300, 'b': 200}
Objektorientierung
>>> class MyClass(object):
... def __init__(self, attr1, attr2):
... self.attr1 = attr1
... self.attr2 = attr2
... def get_sum(self):
... return self.attr1 + self.attr2
...
>>> my_instance = MyClass(10, 12)
>>> my_instance.get_sum()
22
Objektorientierung
>>> my_instance.attr1
10
>>> my_instance.attr2
12
>>> my_instance.attr1 = 100
>>> my_instance.attr1
100
>>> my_instance.get_sum()
112
Zen
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
Zen
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
Fragen?

Python Mike Müller

  • 1.
    Python – Wenigerist Mehr oder Mehr mit Weniger Python Geek Night, Zürich, 16. November 2010 Autor: Dr.-Ing. Mike Müller E-Mail: mmueller@python-academy.de
  • 2.
    Entstehung • 1989/1990 Guidovan Rossum • Mittelweg zwischen C und Shell-Scripting • Ideen von ABC, C, Smalltalk, Java, (Haskell) • Langsame Entwicklung in den 1990ern • 1999 Version 1.5.2 • Python Software Foundation
  • 3.
    Open Source • Python-Lizenz •Python Software Foundation • Kern-Entwickler • Bibliotheks-Entwickler • Benevolent Diktator for Life - BDFL • Python Enhancement Proposals
  • 4.
  • 5.
    Anwender • Open Source •Google (Kern-Entwickler) • YouTube • Deutsche Luft- und Raumfahrt • NASA • Walt Disney • Rackspace • ...
  • 6.
    Anwender • Programmieranfänger • Softwareentwickler •Tester • Systemadminstratoren • Wissenschaftler und Ingenieure • Python skaliert • leichter Einstieg • viele fortgeschrittene Möglichkeiten (Meta-Programmierung)
  • 7.
  • 8.
    Popularität • relativ leichterlernbar • MIT nutzt Python für Computer Science 101 • 1. Semester Python, 2. Semester C++ == 2 Semester C++ • eingebettet in Blender, Open Office, Inkscape, Gimp • macht vielen Leuten einfach Spaß
  • 9.
    Betriebssysteme • Windows • Linux •Mac • Main-Frames • Mobile Geräte • DOS
  • 10.
    Technologie • Interpreter • PlattformunabhängigerBytecode • Fast alles zur Laufzeit • Einfache Meta-Programmierung
  • 11.
    Implementierungen • CPython ==Standard Python • Jython in Java • IronPython in C# • PyPy in Python • Stackless • Mehr
  • 12.
    Python ist keine Insel •Erweitern in C/C++, Java, C# • SWIG, SIP, Boost.Python • .NET, COM • Cython • Einbetten • Zugriff auf DLLS / Shared Libraries
  • 13.
    Kleben • Glue Language •Verbinden von (heterogen Systemen) • Generieren von Eingabe-Daten • Starten und Überwachen von Prozessen • Lesen von Ausgabe-Daten • Kommunikation über das Netzwerk • ... • schnell mit wenigen Code-Zeilen umsetztbar
  • 14.
    Bibliotheken • Reichhaltige Standardbibliothek •Python Package Index ca. 12.000 Pakete • Wrapper für GUI-Toolkits (wxPython, PyQt, Tkinter etc.) • Web-Frameworks (Django, Zope, TurboGears, Pylons etc.) • Zugriff auf alle gängigen Datenbanken • Wissenschaftliche Bibliotheken (NumPy, SciPy, PyTables etc.)
  • 15.
  • 16.
    Syntax • Einrückungen zählen •Compiler "sieht" Quelltext genauso wie Programmierer • Einrückung nach Doppelpunkt • Ausrückung zeigt Endes eines Blockes an • Ausführbarer Pseudocode • Elegant
  • 17.
    Interaktiver Modus >>> 1+ 1 2 >>> def add(x, y): ... return x + y ... >>> add(12, 14) 26
  • 18.
    Scripte # add.py def add(x,y): return x + y print add(12, 14) Ausgabe: 26
  • 19.
    Datenstrukturen • Eingebaut • FesterBestandteil der Sprache • Listen • Dictionarys
  • 20.
    Listen >>> my_list =[10, 20, 30, 40, 50] >>> my_list[0] 10 >>> my_list[1] 20 >>> my_list[-1] 50 >>> my_list[1:3] [20, 30] >>> my_list[::2] [10, 30, 50]
  • 21.
    Listen >>> my_list.append(60) >>> my_list [10,20, 30, 40, 50, 60] >>> my_list.append([1, 2, 3]) >>> my_list [10, 20, 30, 40, 50, 60, [1, 2, 3]] >>> my_list[-1] = 70 >>> my_list [10, 20, 30, 40, 50, 60, 70]
  • 22.
    List Comprehension • vonHaskell abgeschaut >>> [item * 2 for item in my_list] [20, 40, 60, 80, 100, 120, 140] >>> [item * 2 for item in my_list if item > 40] [100, 120, 140] >>>
  • 23.
    Dictionarys • Hash-Tabellen • AssoziativeArrays >>> my_dict = {'a': 100, 'b': 200, 'c': 300} >>> my_dict {'a': 100, 'c': 300, 'b': 200} >>> my_dict['a'] 100
  • 24.
    Dictionarys >>> my_dict['x'] Traceback (mostrecent call last): File "<interactive input>", line 1, in <module> KeyError: 'x' >>> my_dict['x'] = 3 >>> my_dict {'a': 100, 'x': 3, 'c': 300, 'b': 200} >>> my_dict['a'] = -1 >>> my_dict {'a': -1, 'x': 3, 'c': 300, 'b': 200}
  • 25.
    Objektorientierung >>> class MyClass(object): ...def __init__(self, attr1, attr2): ... self.attr1 = attr1 ... self.attr2 = attr2 ... def get_sum(self): ... return self.attr1 + self.attr2 ... >>> my_instance = MyClass(10, 12) >>> my_instance.get_sum() 22
  • 26.
    Objektorientierung >>> my_instance.attr1 10 >>> my_instance.attr2 12 >>>my_instance.attr1 = 100 >>> my_instance.attr1 100 >>> my_instance.get_sum() 112
  • 27.
    Zen >>> import this TheZen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced.
  • 28.
    Zen In the faceof ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
  • 29.