Post on 15-Jan-2016
Python Programming Language
Python Programming Language
Created in 1991 by Guido Van Rossum
Python Programming Language
General Purpose Language Clean Syntax
Python Programming Language
General Purpose Language Clean Syntax Easy to Learn
Python Programming Language
General Purpose Language Clean Syntax Easy to Learn Easy to Debug
Python Programming Language
General Purpose Language Clean Syntax Easy to Learn Easy to Debug “Natural Feeling”
Python Programming Language
General Purpose Language Interpreted
Python Programming Language
General Purpose Language Interpreted
No Compilation Phase
Python Programming Language
General Purpose Language Interpreted
No Compilation Phase Multiplatform Integration
Python Programming Language
General Purpose Language Interpreted
No Compilation Phase Multiplatform Integration Native Debugger
Python Programming Language
General Purpose Language Interpreted Duck Typing
Python Programming Language
General Purpose Language Interpreted Duck Typing
Override behaviours by creating methods
Python Programming Language
General Purpose Language Interpreted Duck Typing
Override behaviours by creating methods Implement operations by creating
methodst
Python Programming Language
General Purpose Language Interpreted Duck Typing
Override behaviours by creating methods Implement operations by creating
methods All data is an object
Python Programming Language
Objects are Python’s 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.
Python Programming Language
An object’s 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
Python Programming Language
An object’s 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 object’s type is also unchangeable. The type() function returns an object’s
type (which is an object itself).
Python Programming Language
General Purpose Language Interpreted Duck Typing Strongly Typed
Python 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.
Python Programming Language
print('hello world') 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)
Functional Example
Pythonaddn = lambda n: lambda x: x + n
Ordef addN(n): def add_n(x): return x + n return add_n
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)))
Modular Design
The standard Python interpreter (CPython) is written in C89
It is designed with two-way interfacing in mind:Embedding C programs in PythonEmbedding Python programs in C
Modular DesignAn Example C Module#include <Python.h>
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( **** 'find . -name "*.py" **** -exec grep -Hn "Flying Circus" {} \;') ***********************************************************/
Cross Platform Execution
The CPython interpreter can be built on most platforms with a standard C library including
glibc and uclibc.
Cross Platform Execution
Interpreters such as Jython and IronPython can be used to run a python interpreter on
any Java or .NET VM respectively.
Python Is Good For
Protyping
Python Is Good For
ProtypingWeb Applications/SAS
Python Is Good For
ProtypingWeb Applications/SAS
Integration
Python Is Good For
ProtypingWeb Applications/SAS
IntegrationTransport Limited Applications
Python Is Good For
ProtypingWeb Applications/SAS
IntegrationTransport Limited ApplicationsIndeterminate Requirements
Python Is Good For
ProtypingWeb Applications/SAS
IntegrationTransport Limited ApplicationsIndeterminate requirements Short Relevence Lifetime
Python Is Good For
ProtypingWeb Applications/SAS
IntegrationTransport Limited ApplicationsIndeterminate requirements Short Relevence Lifetime
Porting Legacy Applications
Python Is Good For
ProtypingWeb Applications/SAS
IntegrationTransport Limited ApplicationsIndeterminate requirements Short Relevence Lifetime
Porting Legacy ApplicationsGlue
Python is Not Good For
Native Cryptography
Python is Not Good For
Native CryptographyMILOR
Python is Not Good For
Native CryptographyMILOR
Highly Parallel Design
__Types__
None
__Types__
None
NotImplemented
__Types__
NoneNotImplemented
Boolean
__Types__
NoneNotImplemented
Boolean
Int/LongInt
__Types__
NoneNotImplemented
BooleanInt/LongInt
Float (which is really a double)
__Types__
NoneNotImplemented
BooleanInt/LongInt
Float (which is really a double)
Complex (double +doubleJ)
__Types__
NoneNotImplemented
BooleanInt/LongInt
Float (which is really a double)Complex (double +doubleJ)
Sequences...
__Types__
Sequencesstring
unicodebytestuplelistset
frozenset
__Types__
NoneNotImplemented
BooleanInt/LongInt
Float (which is really a double)Complex (double +doubleJ)
Sequences...
Mapping Types (dict)
__Types__
NoneNotImplemented
BooleanInt/LongInt
Float (which is really a double)Complex (double +doubleJ)
Sequences...Mapping Types (dict)
Functions and Methods
__Types__
NoneNotImplemented
BooleanInt/LongInt
Float (which is really a double)Complex (double +doubleJ)
Sequences...Mapping Types (dict)
Functions and Methods
Generators
__Types__
NoneNotImplemented
BooleanInt/LongInt
Float (which is really a double)Complex (double +doubleJ)
Sequences...Mapping Types (dict)
Functions and MethodsGenerators
Modules
__Types__
NoneNotImplemented
BooleanInt/LongInt
Float (which is really a double)Complex (double +doubleJ)
Sequences...Mapping Types (dict)
Functions and MethodsGeneratorsModules
File/Buffer
__Types__
NoneNotImplemented
BooleanInt/LongInt
Float (which is really a double)Complex (double +doubleJ)
Sequences...Mapping Types (dict)
Functions and MethodsGeneratorsModules
File/Buffer
Type (metaclasses)
Special Duck Methods__abs__ __add____and____iter__
__getitem____iter____del__
__cmp__!__hash__
__lt__For Example
Example Codeclass Foo: baz = 'monkey' def bar(self): self.printFunc(self.text)
foo = Foo()foo.text = 'Hello World'
def print_console_factory( filter_func=lambda a: a ): def print_console(text): print(filter_func(text)) return print_console
foo.printFunc = print_console_factory()
print_hello_world = foo.barprint_hello_world()
>>> Hello World
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_vowels)
print_hello_world()
>>>Hll Wrld
Python 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
Alternate Implementation
C API http://docs.python.org/extending Create C Modules Execute Python within a C application Interface via a C API
Alternate 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
Alternate Implementation
PyPy http://codespeak.net/pypy/dist/pypy/doc/ Python interpreter written in python Framework interprets multiple languages Highly extendable Slow
Alternate Implementation
Psyco http://psyco.sourceforge.net Actually a C module Produces highly optimized C code from
python bytecode Excellent performance characteristics Configurable
Alternate Implementation
IronPython http://codesplex.com/Wiki/View.aspx?
ProjectName=IronPython 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
Alternate 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
Alternate 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
Alternate 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
Hosting Python
mod_python By far most common hosting mechanism http://modpython.org Apache2 specific Interpreter embedded in webserver
worker Memory intensive Fast
Hosting 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
Hosting Python
FastCGI Mature and stable Requires no 3rd party modules for most
webservers Fast and inexpensive Sandboxing supported
Hosting Python
CGI Mature and stable Supported on nearly all platforms Very flexible in terms of hosting
requirements Slow
Web Frameworks
Django http://www.djangoproject.com/ Active user community Well documented Currently under active development Extensive meta and mock classes Clean layer separation
Web Frameworks
Django Data Layer Business Logic Control Layer Presentation Layer
Not just for the web
Web Frameworks
Turbogears – CherryPy http://www.turbogears.org Persistent app server Javascript integration via mochikit Flexible DB backend via SQLObject
Web 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
Web Frameworks
Zope http://www.zope.org Web Application Framework Highly Web Oriented Not lightweight Highly Featureful ZDB Data store backend
Web Frameworks
Zope http://www.zope.org Web Application Framework Highly Web Oriented Not lightweight Highly Featureful ZDB Data store backend