Transcript Python
Python
www.python.org
What’s in a name?
• Snake logos and mascot
notwithstanding, it’s named after
Monty Python’s Flying Circus
• Humor-impaired can safely ignore
the spam references :-)
• Nobody expects the Spanish
inquisition
What is Python?
• O-O rapid prototyping language
• Not just a scripting language
• Not just another Perl-wannabe
• Extensible (add new modules)
– C/C++/Fortran/whatever
– Java (through JPython)
• Embeddable in applications
Touchy-feely properties
• Free (open source)
– copyrighted but use not restricted
• Mature (9 years old)
• Supportive user community
– & more books in the pipeline!
• Elegant design, easy to learn
– reads like “pseudo-code”
– Suitable as first language
High-level properties
• Extremely portable
– Unix, Windows, Mac, BeOS, Win/CE,
DOS, OS/2, Amiga, VMS, Cray, …
• Compiles to interpreted byte code
– compilation is implicit and automatic
• Memory mgt through ref counting
– easier for C/C++ extensions
• “Safe”: no core dumps
Interfaces to...
• COM, DCOM, ODBC
• Commercial databases
• Java (JPython)
• Many GUI libraries
– platform-independent
• Tk, wxWindows, GTK
– platform-specific
• MFC, MacOS, X11
Language properties
• Everything is an object
• Modules, classes, functions
• Exception handling
• Dynamic typing, polymorphism
• Static scoping
• Operator overloading
• Indentation for block structure
– Otherwise conventional syntax
High-level data types
• Numbers: int, long, float, complex
• Strings: immutable
• Lists and dictionaries: containers
• Other types for e.g. binary data,
regular expressions, introspection
• Extension modules can define new
“built-in” data types
What is it used for?
• rapid prototyping
• web scripting
• throw-away, ad hoc programming
• steering scientific applications
• extension language
• XML processing
• database applications
• GUI applications
Who is using it?
• LLNL, Fermilab (steering)
• Alice project at CMU (3D graphics)
• ObjectDomain (extend UML tool)
• Infoseek (ext. language, scripting)
• Industrial Light & Magic (everything)
• Yahoo! (CGI in Yahoo!mail)
• Digital Creations (Zope website mgt)
• RedHat (Linux installation tools)
Compared to Perl
• Easier to learn
– especially for infrequent users
• More readable code
– improved code maintenance
• Fewer “magical” side effects
• More “safety” guarantees
• Better Java integration
• Some things slower
Compared to Java
• Code 5-10 times more concise
• Dynamic typing
• Much quicker development
– no compilation phase
– less typing
• Yes, it runs slower
– but development is much faster!
Ditto (but more so) for C/C++
Compared to Tcl
• Real datatypes, object-orientation
• More differentiated syntax
• Much faster (even than Tcl 8.0)
• Can use threads
• Less need for C extensions
– hence fewer extension conflicts
• Better Java integration
• Python uses Tk as de-facto GUI std
JPython
• Seamless integration with Java
• Separate implementation
– “classic Python” called CPython here
• Implements the same language
• Different set of standard modules
• differences in “gray areas”
– e.g. some different introspection calls
– different command line options, etc.
Java integration
• Interactive
• Compiles direct to Java bytecode
• Import Java classes directly
• Subclass Java classes
– pass instances back to Java
• Java beans integration
• Can compile into Java class files
– run as applet in browsers
Example function
def gcd(a, b):
“greatest common divisor”
while a != 0:
a, b = b%a, a
# parallel assignment
return b
Example class
class Stack:
“A well-known data structure…” # doc string
def __init__(self): # constructor
self.items = []
def push(self, x):
self.items.append(x) # the sky is the limit
def pop(self):
x = self.items[-1] # what happens if it’s empty?
del self.items[-1]
return x
def empty(self):
return len(self.items) == 0