Python Programming Language. Created in 1991 by Guido Van Rossum.

71
Python Programming Language

Transcript of Python Programming Language. Created in 1991 by Guido Van Rossum.

Page 1: Python Programming Language. Created in 1991 by Guido Van Rossum.

Python Programming Language

Page 2: Python Programming Language. Created in 1991 by Guido Van Rossum.

Python Programming Language

Created in 1991 by Guido Van Rossum

Page 3: Python Programming Language. Created in 1991 by Guido Van Rossum.

Python Programming Language

General Purpose Language Clean Syntax

Page 4: Python Programming Language. Created in 1991 by Guido Van Rossum.

Python Programming Language

General Purpose Language Clean Syntax Easy to Learn

Page 5: Python Programming Language. Created in 1991 by Guido Van Rossum.

Python Programming Language

General Purpose Language Clean Syntax Easy to Learn Easy to Debug

Page 6: Python Programming Language. Created in 1991 by Guido Van Rossum.

Python Programming Language

General Purpose Language Clean Syntax Easy to Learn Easy to Debug “Natural Feeling”

Page 7: Python Programming Language. Created in 1991 by Guido Van Rossum.

Python Programming Language

General Purpose Language Interpreted

Page 8: Python Programming Language. Created in 1991 by Guido Van Rossum.

Python Programming Language

General Purpose Language Interpreted

No Compilation Phase

Page 9: Python Programming Language. Created in 1991 by Guido Van Rossum.

Python Programming Language

General Purpose Language Interpreted

No Compilation Phase Multiplatform Integration

Page 10: Python Programming Language. Created in 1991 by Guido Van Rossum.

Python Programming Language

General Purpose Language Interpreted

No Compilation Phase Multiplatform Integration Native Debugger

Page 11: Python Programming Language. Created in 1991 by Guido Van Rossum.

Python Programming Language

General Purpose Language Interpreted Duck Typing

Page 12: Python Programming Language. Created in 1991 by Guido Van Rossum.

Python Programming Language

General Purpose Language Interpreted Duck Typing

Override behaviours by creating methods

Page 13: Python Programming Language. Created in 1991 by Guido Van Rossum.

Python Programming Language

General Purpose Language Interpreted Duck Typing

Override behaviours by creating methods Implement operations by creating

methodst

Page 14: Python Programming Language. Created in 1991 by Guido Van Rossum.

Python Programming Language

General Purpose Language Interpreted Duck Typing

Override behaviours by creating methods Implement operations by creating

methods All data is an object

Page 15: Python Programming Language. Created in 1991 by Guido Van Rossum.

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.

Page 16: Python Programming Language. Created in 1991 by Guido Van Rossum.

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

Page 17: Python Programming Language. Created in 1991 by Guido Van Rossum.

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).

Page 18: Python Programming Language. Created in 1991 by Guido Van Rossum.

Python Programming Language

General Purpose Language Interpreted Duck Typing Strongly Typed

Page 19: Python Programming Language. Created in 1991 by Guido Van Rossum.

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.

Page 20: Python Programming Language. Created in 1991 by Guido Van Rossum.

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)

Page 21: Python Programming Language. Created in 1991 by Guido Van Rossum.

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)))

Page 22: Python Programming Language. Created in 1991 by Guido Van Rossum.

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

Page 23: Python Programming Language. Created in 1991 by Guido Van Rossum.

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" {} \;') ***********************************************************/

Page 24: Python Programming Language. Created in 1991 by Guido Van Rossum.

Cross Platform Execution

The CPython interpreter can be built on most platforms with a standard C library including

glibc and uclibc.

Page 25: Python Programming Language. Created in 1991 by Guido Van Rossum.

Cross Platform Execution

Interpreters such as Jython and IronPython can be used to run a python interpreter on

any Java or .NET VM respectively.

Page 26: Python Programming Language. Created in 1991 by Guido Van Rossum.

Python Is Good For

Protyping

Page 27: Python Programming Language. Created in 1991 by Guido Van Rossum.

Python Is Good For

ProtypingWeb Applications/SAS

Page 28: Python Programming Language. Created in 1991 by Guido Van Rossum.

Python Is Good For

ProtypingWeb Applications/SAS

Integration

Page 29: Python Programming Language. Created in 1991 by Guido Van Rossum.

Python Is Good For

ProtypingWeb Applications/SAS

IntegrationTransport Limited Applications

Page 30: Python Programming Language. Created in 1991 by Guido Van Rossum.

Python Is Good For

ProtypingWeb Applications/SAS

IntegrationTransport Limited ApplicationsIndeterminate Requirements

Page 31: Python Programming Language. Created in 1991 by Guido Van Rossum.

Python Is Good For

ProtypingWeb Applications/SAS

IntegrationTransport Limited ApplicationsIndeterminate requirements Short Relevence Lifetime

Page 32: Python Programming Language. Created in 1991 by Guido Van Rossum.

Python Is Good For

ProtypingWeb Applications/SAS

IntegrationTransport Limited ApplicationsIndeterminate requirements Short Relevence Lifetime

Porting Legacy Applications

Page 33: Python Programming Language. Created in 1991 by Guido Van Rossum.

Python Is Good For

ProtypingWeb Applications/SAS

IntegrationTransport Limited ApplicationsIndeterminate requirements Short Relevence Lifetime

Porting Legacy ApplicationsGlue

Page 34: Python Programming Language. Created in 1991 by Guido Van Rossum.

Python is Not Good For

Native Cryptography

Page 35: Python Programming Language. Created in 1991 by Guido Van Rossum.

Python is Not Good For

Native CryptographyMILOR

Page 36: Python Programming Language. Created in 1991 by Guido Van Rossum.

Python is Not Good For

Native CryptographyMILOR

Highly Parallel Design

Page 37: Python Programming Language. Created in 1991 by Guido Van Rossum.

__Types__

None

Page 38: Python Programming Language. Created in 1991 by Guido Van Rossum.

__Types__

None

NotImplemented

Page 39: Python Programming Language. Created in 1991 by Guido Van Rossum.

__Types__

NoneNotImplemented

Boolean

Page 40: Python Programming Language. Created in 1991 by Guido Van Rossum.

__Types__

NoneNotImplemented

Boolean

Int/LongInt

Page 41: Python Programming Language. Created in 1991 by Guido Van Rossum.

__Types__

NoneNotImplemented

BooleanInt/LongInt

Float (which is really a double)

Page 42: Python Programming Language. Created in 1991 by Guido Van Rossum.

__Types__

NoneNotImplemented

BooleanInt/LongInt

Float (which is really a double)

Complex (double +doubleJ)

Page 43: Python Programming Language. Created in 1991 by Guido Van Rossum.

__Types__

NoneNotImplemented

BooleanInt/LongInt

Float (which is really a double)Complex (double +doubleJ)

Sequences...

Page 44: Python Programming Language. Created in 1991 by Guido Van Rossum.

__Types__

Sequencesstring

unicodebytestuplelistset

frozenset

Page 45: Python Programming Language. Created in 1991 by Guido Van Rossum.

__Types__

NoneNotImplemented

BooleanInt/LongInt

Float (which is really a double)Complex (double +doubleJ)

Sequences...

Mapping Types (dict)

Page 46: Python Programming Language. Created in 1991 by Guido Van Rossum.

__Types__

NoneNotImplemented

BooleanInt/LongInt

Float (which is really a double)Complex (double +doubleJ)

Sequences...Mapping Types (dict)

Functions and Methods

Page 47: Python Programming Language. Created in 1991 by Guido Van Rossum.

__Types__

NoneNotImplemented

BooleanInt/LongInt

Float (which is really a double)Complex (double +doubleJ)

Sequences...Mapping Types (dict)

Functions and Methods

Generators

Page 48: Python Programming Language. Created in 1991 by Guido Van Rossum.

__Types__

NoneNotImplemented

BooleanInt/LongInt

Float (which is really a double)Complex (double +doubleJ)

Sequences...Mapping Types (dict)

Functions and MethodsGenerators

Modules

Page 49: Python Programming Language. Created in 1991 by Guido Van Rossum.

__Types__

NoneNotImplemented

BooleanInt/LongInt

Float (which is really a double)Complex (double +doubleJ)

Sequences...Mapping Types (dict)

Functions and MethodsGeneratorsModules

File/Buffer

Page 50: Python Programming Language. Created in 1991 by Guido Van Rossum.

__Types__

NoneNotImplemented

BooleanInt/LongInt

Float (which is really a double)Complex (double +doubleJ)

Sequences...Mapping Types (dict)

Functions and MethodsGeneratorsModules

File/Buffer

Type (metaclasses)

Page 51: Python Programming Language. Created in 1991 by Guido Van Rossum.

Special Duck Methods__abs__ __add____and____iter__

__getitem____iter____del__

__cmp__!__hash__

__lt__For Example

Page 52: Python Programming Language. Created in 1991 by Guido Van Rossum.

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

Page 53: Python Programming Language. Created in 1991 by Guido Van Rossum.

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

Page 54: Python Programming Language. Created in 1991 by Guido Van Rossum.

Alternate Implementation

C API http://docs.python.org/extending Create C Modules Execute Python within a C application Interface via a C API

Page 55: Python Programming Language. Created in 1991 by Guido Van Rossum.

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

Page 56: Python Programming Language. Created in 1991 by Guido Van Rossum.

Alternate Implementation

PyPy http://codespeak.net/pypy/dist/pypy/doc/ Python interpreter written in python Framework interprets multiple languages Highly extendable Slow

Page 57: Python Programming Language. Created in 1991 by Guido Van Rossum.

Alternate Implementation

Psyco http://psyco.sourceforge.net Actually a C module Produces highly optimized C code from

python bytecode Excellent performance characteristics Configurable

Page 58: Python Programming Language. Created in 1991 by Guido Van Rossum.

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

Page 59: Python Programming Language. Created in 1991 by Guido Van Rossum.

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

Page 60: Python Programming Language. Created in 1991 by Guido Van Rossum.

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

Page 61: Python Programming Language. Created in 1991 by Guido Van Rossum.

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

Page 62: Python Programming Language. Created in 1991 by Guido Van Rossum.

Hosting Python

mod_python By far most common hosting mechanism http://modpython.org Apache2 specific Interpreter embedded in webserver

worker Memory intensive Fast

Page 63: Python Programming Language. Created in 1991 by Guido Van Rossum.

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

Page 64: Python Programming Language. Created in 1991 by Guido Van Rossum.

Hosting Python

FastCGI Mature and stable Requires no 3rd party modules for most

webservers Fast and inexpensive Sandboxing supported

Page 65: Python Programming Language. Created in 1991 by Guido Van Rossum.

Hosting Python

CGI Mature and stable Supported on nearly all platforms Very flexible in terms of hosting

requirements Slow

Page 66: Python Programming Language. Created in 1991 by Guido Van Rossum.

Web Frameworks

Django http://www.djangoproject.com/ Active user community Well documented Currently under active development Extensive meta and mock classes Clean layer separation

Page 67: Python Programming Language. Created in 1991 by Guido Van Rossum.

Web Frameworks

Django Data Layer Business Logic Control Layer Presentation Layer

Not just for the web

Page 68: Python Programming Language. Created in 1991 by Guido Van Rossum.

Web Frameworks

Turbogears – CherryPy http://www.turbogears.org Persistent app server Javascript integration via mochikit Flexible DB backend via SQLObject

Page 69: Python Programming Language. Created in 1991 by Guido Van Rossum.

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

Page 70: Python Programming Language. Created in 1991 by Guido Van Rossum.

Web Frameworks

Zope http://www.zope.org Web Application Framework Highly Web Oriented Not lightweight Highly Featureful ZDB Data store backend

Page 71: Python Programming Language. Created in 1991 by Guido Van Rossum.

Web Frameworks

Zope http://www.zope.org Web Application Framework Highly Web Oriented Not lightweight Highly Featureful ZDB Data store backend