Title: Python Programming Language
1Python Programming Language
2Python Programming Language
- Created in 1991 by Guido Van Rossum
3Python Programming Language
- General Purpose Language
- Clean Syntax
4Python Programming Language
- General Purpose Language
- Clean Syntax
- Easy to Learn
5Python Programming Language
- General Purpose Language
- Clean Syntax
- Easy to Learn
- Easy to Debug
6Python Programming Language
- General Purpose Language
- Clean Syntax
- Easy to Learn
- Easy to Debug
- Natural Feeling
7Python Programming Language
- General Purpose Language
- Interpreted
8Python Programming Language
- General Purpose Language
- Interpreted
- No Compilation Phase
9Python Programming Language
- General Purpose Language
- Interpreted
- No Compilation Phase
- Multiplatform Integration
10Python Programming Language
- General Purpose Language
- Interpreted
- No Compilation Phase
- Multiplatform Integration
- Native Debugger
11Python Programming Language
- General Purpose Language
- Interpreted
- Duck Typing
12Python Programming Language
- General Purpose Language
- Interpreted
- Duck Typing
- Override behaviours by creating methods
13Python Programming Language
- General Purpose Language
- Interpreted
- Duck Typing
- Override behaviours by creating methods
- Implement operations by creating methodst
14Python Programming Language
- General Purpose Language
- Interpreted
- Duck Typing
- Override behaviours by creating methods
- Implement operations by creating methods
- All data is an object
15Python Programming Language
- Objects are Pythons abstraction for data. All
data in a Python program is represented by
objects or by relations between objects. - Every object has an identity, a type and a value.
16Python Programming Language
- An objects identity never changes once it has
been created - The is operator compares the identity of two
objects - The id() function returns an integer representing
its identity
17Python Programming Language
- An objects identity never changes once it has
been created - The is operator compares the identity of two
objects - The id() function returns an integer representing
its identity - An objects type is also unchangeable.
- The type() function returns an objects type
(which is an object itself).
18Python Programming Language
- General Purpose Language
- Interpreted
- Duck Typing
- Strongly Typed
19Python Programming Language
- A Python programmer can write in any style they
like, using design patterns borrowed from - Imperative
- Declarative
- Object Oriented
- functional programming
- The author is free let the problem guide the
development of the solution.
20Python Programming Language
- class Hello(object)
- def __init__(self, my_string)
- self.my_string my_string
- def __call__(self, render_func)
- out_str 'Hello s' self.my_string
- render_func(out_str)
- def print_string(string_to_print)
- print(string_to_print)
- myHelloWorldClass Hello('world')
- myHelloWorldClass(print_string)
21Functional Example
- Java
- public class OuterClass
- // Inner class
- class AddN
- AddN(int n) _n n
- int add(int v) return _n v
- private int _n
-
- public AddN createAddN(int var)
- return new AddN(var)
-
-
- LISP
- (define (addn n) (lambda (k) ( n k)))
Python addn lambda n lambda x x n Or def
addN(n) def add_n(x) return x n
return add_n
22Modular Design
- The standard Python interpreter (CPython) is
written in C89 - It is designed with two-way interfacing in mind
- Embedding C programs in Python
- Embedding Python programs in C
23Modular Design
- An Example C Module
- include ltPython.hgt
- static PyObject
- spam_system(PyObject self, PyObject args)
-
- const char command
- int sts
- if (!PyArg_ParseTuple(args, "s", command))
- return NULL
- sts system(command)
- return Py_BuildValue("i", sts)
-
- /
- import spam
- spam.system(
24Cross Platform Execution
- The CPython interpreter can be built on most
platforms with a standard C library including
glibc and uclibc.
25Cross Platform Execution
- Interpreters such as Jython and IronPython can be
used to run a python interpreter on any Java or
.NET VM respectively.
26Python Is Good For
27Python Is Good For
- Protyping
- Web Applications/SAS
28Python Is Good For
- Protyping
- Web Applications/SAS
- Integration
29Python Is Good For
- Protyping
- Web Applications/SAS
- Integration
- Transport Limited Applications
30Python Is Good For
- Protyping
- Web Applications/SAS
- Integration
- Transport Limited Applications
- Indeterminate Requirements
31Python Is Good For
- Protyping
- Web Applications/SAS
- Integration
- Transport Limited Applications
- Indeterminate requirements
- Short Relevence Lifetime
32Python Is Good For
- Protyping
- Web Applications/SAS
- Integration
- Transport Limited Applications
- Indeterminate requirements
- Short Relevence Lifetime
- Porting Legacy Applications
33Python Is Good For
- Protyping
- Web Applications/SAS
- Integration
- Transport Limited Applications
- Indeterminate requirements
- Short Relevence Lifetime
- Porting Legacy Applications
- Glue
34Python is Not Good For
35Python is Not Good For
- Native Cryptography
- MILOR
36Python is Not Good For
- Native Cryptography
- MILOR
- Highly Parallel Design
37__Types__
38__Types__
39__Types__
- None
- NotImplemented
- Boolean
40__Types__
- None
- NotImplemented
- Boolean
- Int/LongInt
41__Types__
- None
- NotImplemented
- Boolean
- Int/LongInt
- Float (which is really a double)
42__Types__
- None
- NotImplemented
- Boolean
- Int/LongInt
- Float (which is really a double)
- Complex (double doubleJ)
43__Types__
- None
- NotImplemented
- Boolean
- Int/LongInt
- Float (which is really a double)
- Complex (double doubleJ)
- Sequences...
44__Types__
- Sequences
- string
- unicode
- bytes
- tuple
- list
- set
- frozenset
45__Types__
- None
- NotImplemented
- Boolean
- Int/LongInt
- Float (which is really a double)
- Complex (double doubleJ)
- Sequences...
- Mapping Types (dict)
46__Types__
- None
- NotImplemented
- Boolean
- Int/LongInt
- Float (which is really a double)
- Complex (double doubleJ)
- Sequences...
- Mapping Types (dict)
- Functions and Methods
47__Types__
- None
- NotImplemented
- Boolean
- Int/LongInt
- Float (which is really a double)
- Complex (double doubleJ)
- Sequences...
- Mapping Types (dict)
- Functions and Methods
- Generators
48__Types__
- None
- NotImplemented
- Boolean
- Int/LongInt
- Float (which is really a double)
- Complex (double doubleJ)
- Sequences...
- Mapping Types (dict)
- Functions and Methods
- Generators
- Modules
49__Types__
- None
- NotImplemented
- Boolean
- Int/LongInt
- Float (which is really a double)
- Complex (double doubleJ)
- Sequences...
- Mapping Types (dict)
- Functions and Methods
- Generators
- Modules
- File/Buffer
50__Types__
- None
- NotImplemented
- Boolean
- Int/LongInt
- Float (which is really a double)
- Complex (double doubleJ)
- Sequences...
- Mapping Types (dict)
- Functions and Methods
- Generators
- Modules
- File/Buffer
- Type (metaclasses)
51Special Duck Methods
- __abs__
- __add__
- __and__
- __iter__
- __getitem__
- __iter__
- __del__
- __cmp__!
- __hash__
- __lt__
- For Example
52Example Code
- class Foo
- baz 'monkey'
- def bar(self)
- self.printFunc(self.text)
- foo Foo()
- foo.text 'Hello World'
- def print_console_factory(
- filter_funclambda a a
- )
- def print_console(text)
- print(filter_func(text))
- return print_console
- foo.printFunc
- print_console_factory()
- print_hello_world foo.bar
- vowels 'a', 'e', 'i', 'o', 'u'
- filter_vowels lambda a
- ''.join( let
- for let in a
- if not
- let.lower() in vowels
- )
- foo.printFunc print_console_factory(filter_vowel
s) - print_hello_world()
- gtgtgtHll Wrld
53Python Resources
- Python.org Documentation
- http//www.python.org
- Python.org PEPs
- http//www.python.org/dev/peps/
- Ye Olde Cheese Shoppe
- http//pypi.python.org/pypi
54Alternate Implementation
- C API
- http//docs.python.org/extending
- Create C Modules
- Execute Python within a C application
- Interface via a C API
55Alternate Implementation
- Jython
- http//www.jython.org/Project
- Native JVM Python interpreter
- Full support for standard library
- Other C Extensions may not be ported
- Python extensions may rely on C extensions
56Alternate Implementation
- PyPy
- http//codespeak.net/pypy/dist/pypy/doc/
- Python interpreter written in python
- Framework interprets multiple languages
- Highly extendable
- Slow
57Alternate Implementation
- Psyco
- http//psyco.sourceforge.net
- Actually a C module
- Produces highly optimized C code from python
bytecode - Excellent performance characteristics
- Configurable
58Alternate Implementation
- IronPython
- http//codesplex.com/Wiki/View.aspx?ProjectNameIr
onPython - Native python interpreter (C) for .NET
- Full support for standard library
- Many external modules have been ported
- Porting modules is quite simple
- Can integrate with .NET languages
59Alternate Implementation
- PyJamas
- http//code.google.com/p/pyjamas/
- Python interpreter for JavaScript
- Cross browser fully supported
- As lightweight as you'd think
- JSON/JQuery may be a better option
60Alternate Implementation
- ShedSkin
- http//code.google.com/p/shedskin/
- Produces C code from Python code
- Excellent for prototyping
- Some language features not supported
- Implicit static typed code only
61Alternate Implementation
- Cython
- http//www.cython.org
- Embed C code in a python application
- Excellent for use in profiling
- Compiled at first runtime
- Shared build env with python interpreter
62Hosting Python
- mod_python
- By far most common hosting mechanism
- http//modpython.org
- Apache2 specific
- Interpreter embedded in webserver worker
- Memory intensive
- Fast
63Hosting Python
- WSGI
- Up and coming for a good reason
- http//code.google.com/p/modwsgi/
- http//code.google.com/p/isapi-wsgi/
- Can embedded interpreter
- Can run threaded standalong app server
- Very fast and inexpensive
- Sandboxing supported
64Hosting Python
- FastCGI
- Mature and stable
- Requires no 3rd party modules for most webservers
- Fast and inexpensive
- Sandboxing supported
65Hosting Python
- CGI
- Mature and stable
- Supported on nearly all platforms
- Very flexible in terms of hosting requirements
- Slow
66Web Frameworks
- Django
- http//www.djangoproject.com/
- Active user community
- Well documented
- Currently under active development
- Extensive meta and mock classes
- Clean layer separation
67Web Frameworks
- Django
- Data Layer
- Business Logic
- Control Layer
- Presentation Layer
- Not just for the web
68Web Frameworks
- Turbogears CherryPy
- http//www.turbogears.org
- Persistent app server
- Javascript integration via mochikit
- Flexible DB backend via SQLObject
69Web Frameworks
- Pylons - Paste
- http//www.pylonshq.org/
- Multiple DB Backends supported
- Multiple templating languages pluggable
- Multiple request dispatching
- HTTP oriented
- Forward compatible
- MVC Type layer separation
70Web Frameworks
- Zope
- http//www.zope.org
- Web Application Framework
- Highly Web Oriented
- Not lightweight
- Highly Featureful
- ZDB Data store backend
71Web Frameworks
- Zope
- http//www.zope.org
- Web Application Framework
- Highly Web Oriented
- Not lightweight
- Highly Featureful
- ZDB Data store backend