1. Python: the Project, the Language and the Style
Juan Manuel Gimeno Illa
jmgimeno@diei.udl.cat
October 2008
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 1 / 28
2. Outline
1 Introduction
2 Python, the project
3 Python, the language
References
Functions
Modules and Packages
Namespaces and Scopes
4 Python, the style
5 Hands on Work
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 2 / 28
3. Introduction
What this session pretends
A very light introduction to the making of Python (the project)
A presentation of some aspects of Python (the language)
We will focus on those elements and concepts that are most shocking
for people coming from C, C++, Java, . . .
A basic knowledge of the language is assumed (a casual reading the
Python Tutorial is enough)
We will begin to get into what me might call the python style of
doing things (the style)
(In the live session we will also present the interpreter (python), an
enhanced interpreter (ipython) and the default integrated
development environment (idle) among other things)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 3 / 28
4. Introduction
What this session pretends
A very light introduction to the making of Python (the project)
A presentation of some aspects of Python (the language)
We will focus on those elements and concepts that are most shocking
for people coming from C, C++, Java, . . .
A basic knowledge of the language is assumed (a casual reading the
Python Tutorial is enough)
We will begin to get into what me might call the python style of
doing things (the style)
(In the live session we will also present the interpreter (python), an
enhanced interpreter (ipython) and the default integrated
development environment (idle) among other things)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 3 / 28
5. Introduction
What this session pretends
A very light introduction to the making of Python (the project)
A presentation of some aspects of Python (the language)
We will focus on those elements and concepts that are most shocking
for people coming from C, C++, Java, . . .
A basic knowledge of the language is assumed (a casual reading the
Python Tutorial is enough)
We will begin to get into what me might call the python style of
doing things (the style)
(In the live session we will also present the interpreter (python), an
enhanced interpreter (ipython) and the default integrated
development environment (idle) among other things)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 3 / 28
6. Introduction
What this session pretends
A very light introduction to the making of Python (the project)
A presentation of some aspects of Python (the language)
We will focus on those elements and concepts that are most shocking
for people coming from C, C++, Java, . . .
A basic knowledge of the language is assumed (a casual reading the
Python Tutorial is enough)
We will begin to get into what me might call the python style of
doing things (the style)
(In the live session we will also present the interpreter (python), an
enhanced interpreter (ipython) and the default integrated
development environment (idle) among other things)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 3 / 28
7. Introduction
What this session pretends
A very light introduction to the making of Python (the project)
A presentation of some aspects of Python (the language)
We will focus on those elements and concepts that are most shocking
for people coming from C, C++, Java, . . .
A basic knowledge of the language is assumed (a casual reading the
Python Tutorial is enough)
We will begin to get into what me might call the python style of
doing things (the style)
(In the live session we will also present the interpreter (python), an
enhanced interpreter (ipython) and the default integrated
development environment (idle) among other things)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 3 / 28
8. Introduction
What this session pretends
A very light introduction to the making of Python (the project)
A presentation of some aspects of Python (the language)
We will focus on those elements and concepts that are most shocking
for people coming from C, C++, Java, . . .
A basic knowledge of the language is assumed (a casual reading the
Python Tutorial is enough)
We will begin to get into what me might call the python style of
doing things (the style)
(In the live session we will also present the interpreter (python), an
enhanced interpreter (ipython) and the default integrated
development environment (idle) among other things)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 3 / 28
9. Python, the project
Organization
Initiated by Guido van Rossum (BDFL: Benevolent Dictator For Life)
in 1990 and made public in 1991
By the years, a process and an infrastructure for the development of
Python have emerged:
SourceForge: patches, bugs, etc.
Mailing lists: python-dev, python-list
Newsgroup: comp.lang.python
PEPs: Python Enhancement Proposals
SIGs: Special Interest Groups
PSF: Python Software Foundation holds the copyright of
Python since version 2.1
All of this is accessible from http://python.org
Its a very open project provided you follow some rules!!
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 4 / 28
10. Python, the project
Organization
Initiated by Guido van Rossum (BDFL: Benevolent Dictator For Life)
in 1990 and made public in 1991
By the years, a process and an infrastructure for the development of
Python have emerged:
SourceForge: patches, bugs, etc.
Mailing lists: python-dev, python-list
Newsgroup: comp.lang.python
PEPs: Python Enhancement Proposals
SIGs: Special Interest Groups
PSF: Python Software Foundation holds the copyright of
Python since version 2.1
All of this is accessible from http://python.org
Its a very open project provided you follow some rules!!
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 4 / 28
11. Python, the project
Organization
Initiated by Guido van Rossum (BDFL: Benevolent Dictator For Life)
in 1990 and made public in 1991
By the years, a process and an infrastructure for the development of
Python have emerged:
SourceForge: patches, bugs, etc.
Mailing lists: python-dev, python-list
Newsgroup: comp.lang.python
PEPs: Python Enhancement Proposals
SIGs: Special Interest Groups
PSF: Python Software Foundation holds the copyright of
Python since version 2.1
All of this is accessible from http://python.org
Its a very open project provided you follow some rules!!
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 4 / 28
12. Python, the project
Organization
Initiated by Guido van Rossum (BDFL: Benevolent Dictator For Life)
in 1990 and made public in 1991
By the years, a process and an infrastructure for the development of
Python have emerged:
SourceForge: patches, bugs, etc.
Mailing lists: python-dev, python-list
Newsgroup: comp.lang.python
PEPs: Python Enhancement Proposals
SIGs: Special Interest Groups
PSF: Python Software Foundation holds the copyright of
Python since version 2.1
All of this is accessible from http://python.org
Its a very open project provided you follow some rules!!
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 4 / 28
13. Python, the project
Organization
Initiated by Guido van Rossum (BDFL: Benevolent Dictator For Life)
in 1990 and made public in 1991
By the years, a process and an infrastructure for the development of
Python have emerged:
SourceForge: patches, bugs, etc.
Mailing lists: python-dev, python-list
Newsgroup: comp.lang.python
PEPs: Python Enhancement Proposals
SIGs: Special Interest Groups
PSF: Python Software Foundation holds the copyright of
Python since version 2.1
All of this is accessible from http://python.org
Its a very open project provided you follow some rules!!
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 4 / 28
14. Python, the project
Organization
Initiated by Guido van Rossum (BDFL: Benevolent Dictator For Life)
in 1990 and made public in 1991
By the years, a process and an infrastructure for the development of
Python have emerged:
SourceForge: patches, bugs, etc.
Mailing lists: python-dev, python-list
Newsgroup: comp.lang.python
PEPs: Python Enhancement Proposals
SIGs: Special Interest Groups
PSF: Python Software Foundation holds the copyright of
Python since version 2.1
All of this is accessible from http://python.org
Its a very open project provided you follow some rules!!
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 4 / 28
15. Python, the project
Organization
Initiated by Guido van Rossum (BDFL: Benevolent Dictator For Life)
in 1990 and made public in 1991
By the years, a process and an infrastructure for the development of
Python have emerged:
SourceForge: patches, bugs, etc.
Mailing lists: python-dev, python-list
Newsgroup: comp.lang.python
PEPs: Python Enhancement Proposals
SIGs: Special Interest Groups
PSF: Python Software Foundation holds the copyright of
Python since version 2.1
All of this is accessible from http://python.org
Its a very open project provided you follow some rules!!
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 4 / 28
16. Python, the project
Organization
Initiated by Guido van Rossum (BDFL: Benevolent Dictator For Life)
in 1990 and made public in 1991
By the years, a process and an infrastructure for the development of
Python have emerged:
SourceForge: patches, bugs, etc.
Mailing lists: python-dev, python-list
Newsgroup: comp.lang.python
PEPs: Python Enhancement Proposals
SIGs: Special Interest Groups
PSF: Python Software Foundation holds the copyright of
Python since version 2.1
All of this is accessible from http://python.org
Its a very open project provided you follow some rules!!
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 4 / 28
17. Python, the project
Organization
Initiated by Guido van Rossum (BDFL: Benevolent Dictator For Life)
in 1990 and made public in 1991
By the years, a process and an infrastructure for the development of
Python have emerged:
SourceForge: patches, bugs, etc.
Mailing lists: python-dev, python-list
Newsgroup: comp.lang.python
PEPs: Python Enhancement Proposals
SIGs: Special Interest Groups
PSF: Python Software Foundation holds the copyright of
Python since version 2.1
All of this is accessible from http://python.org
Its a very open project provided you follow some rules!!
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 4 / 28
18. Python, the project
Organization
Initiated by Guido van Rossum (BDFL: Benevolent Dictator For Life)
in 1990 and made public in 1991
By the years, a process and an infrastructure for the development of
Python have emerged:
SourceForge: patches, bugs, etc.
Mailing lists: python-dev, python-list
Newsgroup: comp.lang.python
PEPs: Python Enhancement Proposals
SIGs: Special Interest Groups
PSF: Python Software Foundation holds the copyright of
Python since version 2.1
All of this is accessible from http://python.org
Its a very open project provided you follow some rules!!
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 4 / 28
19. Python, the project
Implementations
The language Python has different implementations
CPython: Standard implementation of the python language in C.
Currently at version 2.6 of the language
Jython: Java implementation that compiles python code to JVM
byte-code
Currently at version 2.2.1 of the language (alpha version
2.5a3)
IronPython: .NET implementation that compiles python code to CLR
Currently at version 2.4 of the language (with minor
differences)
Stackless: enhanced version using micro-threads, implemented in C
Currently at version 2.6 of the language
PyPy: Rather experimental version of python written in python
Python 3000: (or py3k) non-backwards compatible evolution of CPython
We will use CPython versions 2.4 (very little differences)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 5 / 28
20. Python, the project
Implementations
The language Python has different implementations
CPython: Standard implementation of the python language in C.
Currently at version 2.6 of the language
Jython: Java implementation that compiles python code to JVM
byte-code
Currently at version 2.2.1 of the language (alpha version
2.5a3)
IronPython: .NET implementation that compiles python code to CLR
Currently at version 2.4 of the language (with minor
differences)
Stackless: enhanced version using micro-threads, implemented in C
Currently at version 2.6 of the language
PyPy: Rather experimental version of python written in python
Python 3000: (or py3k) non-backwards compatible evolution of CPython
We will use CPython versions 2.4 (very little differences)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 5 / 28
21. Python, the project
Implementations
The language Python has different implementations
CPython: Standard implementation of the python language in C.
Currently at version 2.6 of the language
Jython: Java implementation that compiles python code to JVM
byte-code
Currently at version 2.2.1 of the language (alpha version
2.5a3)
IronPython: .NET implementation that compiles python code to CLR
Currently at version 2.4 of the language (with minor
differences)
Stackless: enhanced version using micro-threads, implemented in C
Currently at version 2.6 of the language
PyPy: Rather experimental version of python written in python
Python 3000: (or py3k) non-backwards compatible evolution of CPython
We will use CPython versions 2.4 (very little differences)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 5 / 28
22. Python, the project
Implementations
The language Python has different implementations
CPython: Standard implementation of the python language in C.
Currently at version 2.6 of the language
Jython: Java implementation that compiles python code to JVM
byte-code
Currently at version 2.2.1 of the language (alpha version
2.5a3)
IronPython: .NET implementation that compiles python code to CLR
Currently at version 2.4 of the language (with minor
differences)
Stackless: enhanced version using micro-threads, implemented in C
Currently at version 2.6 of the language
PyPy: Rather experimental version of python written in python
Python 3000: (or py3k) non-backwards compatible evolution of CPython
We will use CPython versions 2.4 (very little differences)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 5 / 28
23. Python, the project
Implementations
The language Python has different implementations
CPython: Standard implementation of the python language in C.
Currently at version 2.6 of the language
Jython: Java implementation that compiles python code to JVM
byte-code
Currently at version 2.2.1 of the language (alpha version
2.5a3)
IronPython: .NET implementation that compiles python code to CLR
Currently at version 2.4 of the language (with minor
differences)
Stackless: enhanced version using micro-threads, implemented in C
Currently at version 2.6 of the language
PyPy: Rather experimental version of python written in python
Python 3000: (or py3k) non-backwards compatible evolution of CPython
We will use CPython versions 2.4 (very little differences)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 5 / 28
24. Python, the project
Implementations
The language Python has different implementations
CPython: Standard implementation of the python language in C.
Currently at version 2.6 of the language
Jython: Java implementation that compiles python code to JVM
byte-code
Currently at version 2.2.1 of the language (alpha version
2.5a3)
IronPython: .NET implementation that compiles python code to CLR
Currently at version 2.4 of the language (with minor
differences)
Stackless: enhanced version using micro-threads, implemented in C
Currently at version 2.6 of the language
PyPy: Rather experimental version of python written in python
Python 3000: (or py3k) non-backwards compatible evolution of CPython
We will use CPython versions 2.4 (very little differences)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 5 / 28
25. Python, the project
Implementations
The language Python has different implementations
CPython: Standard implementation of the python language in C.
Currently at version 2.6 of the language
Jython: Java implementation that compiles python code to JVM
byte-code
Currently at version 2.2.1 of the language (alpha version
2.5a3)
IronPython: .NET implementation that compiles python code to CLR
Currently at version 2.4 of the language (with minor
differences)
Stackless: enhanced version using micro-threads, implemented in C
Currently at version 2.6 of the language
PyPy: Rather experimental version of python written in python
Python 3000: (or py3k) non-backwards compatible evolution of CPython
We will use CPython versions 2.4 (very little differences)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 5 / 28
26. Python, the project
Implementations
The language Python has different implementations
CPython: Standard implementation of the python language in C.
Currently at version 2.6 of the language
Jython: Java implementation that compiles python code to JVM
byte-code
Currently at version 2.2.1 of the language (alpha version
2.5a3)
IronPython: .NET implementation that compiles python code to CLR
Currently at version 2.4 of the language (with minor
differences)
Stackless: enhanced version using micro-threads, implemented in C
Currently at version 2.6 of the language
PyPy: Rather experimental version of python written in python
Python 3000: (or py3k) non-backwards compatible evolution of CPython
We will use CPython versions 2.4 (very little differences)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 5 / 28
27. Python, the project
Implementations
The language Python has different implementations
CPython: Standard implementation of the python language in C.
Currently at version 2.6 of the language
Jython: Java implementation that compiles python code to JVM
byte-code
Currently at version 2.2.1 of the language (alpha version
2.5a3)
IronPython: .NET implementation that compiles python code to CLR
Currently at version 2.4 of the language (with minor
differences)
Stackless: enhanced version using micro-threads, implemented in C
Currently at version 2.6 of the language
PyPy: Rather experimental version of python written in python
Python 3000: (or py3k) non-backwards compatible evolution of CPython
We will use CPython versions 2.4 (very little differences)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 5 / 28
28. Python, the language References
Variables and References
A Python program access data values through references
A reference is a name that refers to the location in memory of a value
(object)
(Python classes, functions and methods are also objects)
References takes the form of variables (x), attributes (x.y) and items
(x[y])
A reference has no intrinsic type but gets the type of the referenced
object (duck typing)
The process of linking a reference to a value is called binding
References can be rebound to another object
Statements that create/modify bindings are assignment, def, class
and import
The del statement unbinds the reference (does not deletes the
object)
Bindings live in name-spaces (more on this later)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 6 / 28
29. Python, the language References
Variables and References
A Python program access data values through references
A reference is a name that refers to the location in memory of a value
(object)
(Python classes, functions and methods are also objects)
References takes the form of variables (x), attributes (x.y) and items
(x[y])
A reference has no intrinsic type but gets the type of the referenced
object (duck typing)
The process of linking a reference to a value is called binding
References can be rebound to another object
Statements that create/modify bindings are assignment, def, class
and import
The del statement unbinds the reference (does not deletes the
object)
Bindings live in name-spaces (more on this later)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 6 / 28
30. Python, the language References
Variables and References
A Python program access data values through references
A reference is a name that refers to the location in memory of a value
(object)
(Python classes, functions and methods are also objects)
References takes the form of variables (x), attributes (x.y) and items
(x[y])
A reference has no intrinsic type but gets the type of the referenced
object (duck typing)
The process of linking a reference to a value is called binding
References can be rebound to another object
Statements that create/modify bindings are assignment, def, class
and import
The del statement unbinds the reference (does not deletes the
object)
Bindings live in name-spaces (more on this later)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 6 / 28
31. Python, the language References
Variables and References
A Python program access data values through references
A reference is a name that refers to the location in memory of a value
(object)
(Python classes, functions and methods are also objects)
References takes the form of variables (x), attributes (x.y) and items
(x[y])
A reference has no intrinsic type but gets the type of the referenced
object (duck typing)
The process of linking a reference to a value is called binding
References can be rebound to another object
Statements that create/modify bindings are assignment, def, class
and import
The del statement unbinds the reference (does not deletes the
object)
Bindings live in name-spaces (more on this later)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 6 / 28
32. Python, the language References
Variables and References
A Python program access data values through references
A reference is a name that refers to the location in memory of a value
(object)
(Python classes, functions and methods are also objects)
References takes the form of variables (x), attributes (x.y) and items
(x[y])
A reference has no intrinsic type but gets the type of the referenced
object (duck typing)
The process of linking a reference to a value is called binding
References can be rebound to another object
Statements that create/modify bindings are assignment, def, class
and import
The del statement unbinds the reference (does not deletes the
object)
Bindings live in name-spaces (more on this later)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 6 / 28
33. Python, the language References
Variables and References
A Python program access data values through references
A reference is a name that refers to the location in memory of a value
(object)
(Python classes, functions and methods are also objects)
References takes the form of variables (x), attributes (x.y) and items
(x[y])
A reference has no intrinsic type but gets the type of the referenced
object (duck typing)
The process of linking a reference to a value is called binding
References can be rebound to another object
Statements that create/modify bindings are assignment, def, class
and import
The del statement unbinds the reference (does not deletes the
object)
Bindings live in name-spaces (more on this later)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 6 / 28
34. Python, the language References
Variables and References
A Python program access data values through references
A reference is a name that refers to the location in memory of a value
(object)
(Python classes, functions and methods are also objects)
References takes the form of variables (x), attributes (x.y) and items
(x[y])
A reference has no intrinsic type but gets the type of the referenced
object (duck typing)
The process of linking a reference to a value is called binding
References can be rebound to another object
Statements that create/modify bindings are assignment, def, class
and import
The del statement unbinds the reference (does not deletes the
object)
Bindings live in name-spaces (more on this later)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 6 / 28
35. Python, the language References
Variables and References
A Python program access data values through references
A reference is a name that refers to the location in memory of a value
(object)
(Python classes, functions and methods are also objects)
References takes the form of variables (x), attributes (x.y) and items
(x[y])
A reference has no intrinsic type but gets the type of the referenced
object (duck typing)
The process of linking a reference to a value is called binding
References can be rebound to another object
Statements that create/modify bindings are assignment, def, class
and import
The del statement unbinds the reference (does not deletes the
object)
Bindings live in name-spaces (more on this later)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 6 / 28
36. Python, the language References
Variables and References
A Python program access data values through references
A reference is a name that refers to the location in memory of a value
(object)
(Python classes, functions and methods are also objects)
References takes the form of variables (x), attributes (x.y) and items
(x[y])
A reference has no intrinsic type but gets the type of the referenced
object (duck typing)
The process of linking a reference to a value is called binding
References can be rebound to another object
Statements that create/modify bindings are assignment, def, class
and import
The del statement unbinds the reference (does not deletes the
object)
Bindings live in name-spaces (more on this later)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 6 / 28
37. Python, the language References
Variables and References
A Python program access data values through references
A reference is a name that refers to the location in memory of a value
(object)
(Python classes, functions and methods are also objects)
References takes the form of variables (x), attributes (x.y) and items
(x[y])
A reference has no intrinsic type but gets the type of the referenced
object (duck typing)
The process of linking a reference to a value is called binding
References can be rebound to another object
Statements that create/modify bindings are assignment, def, class
and import
The del statement unbinds the reference (does not deletes the
object)
Bindings live in name-spaces (more on this later)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 6 / 28
38. Python, the language References
Binding, Rebinding and Unbinding
>>> l = []
>>> id(l)
-1210453844
l gets bound though assignment to a list
(id returns the identity of an object)
l maintains its identity through method
application
But gets rebound with assignment
In this case, augmented assignment does
not rebind
del statement unbinds the reference (it
does not delete objects!!)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 7 / 28
39. Python, the language References
Binding, Rebinding and Unbinding
>>> l = []
>>> id(l)
-1210453844
>>> l.append(1)
l gets bound though assignment to a list >>> id(l)
(id returns the identity of an object) -1210453844
l maintains its identity through method
application
But gets rebound with assignment
In this case, augmented assignment does
not rebind
del statement unbinds the reference (it
does not delete objects!!)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 7 / 28
40. Python, the language References
Binding, Rebinding and Unbinding
>>> l = []
>>> id(l)
-1210453844
>>> l.append(1)
l gets bound though assignment to a list >>> id(l)
(id returns the identity of an object) -1210453844
l maintains its identity through method >>> l = l + [2]
application >>> id(l)
But gets rebound with assignment -1210467412
In this case, augmented assignment does
not rebind
del statement unbinds the reference (it
does not delete objects!!)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 7 / 28
41. Python, the language References
Binding, Rebinding and Unbinding
>>> l = []
>>> id(l)
-1210453844
>>> l.append(1)
l gets bound though assignment to a list >>> id(l)
(id returns the identity of an object) -1210453844
l maintains its identity through method >>> l = l + [2]
application >>> id(l)
But gets rebound with assignment -1210467412
In this case, augmented assignment does >>> l += [3]
not rebind >>> id(l)
del statement unbinds the reference (it -1210467412
does not delete objects!!)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 7 / 28
42. Python, the language References
Binding, Rebinding and Unbinding
>>> l = []
>>> id(l)
-1210453844
>>> l.append(1)
l gets bound though assignment to a list >>> id(l)
(id returns the identity of an object) -1210453844
l maintains its identity through method >>> l = l + [2]
application >>> id(l)
But gets rebound with assignment -1210467412
In this case, augmented assignment does >>> l += [3]
not rebind >>> id(l)
del statement unbinds the reference (it -1210467412
does not delete objects!!) >>> del l
>>> id(l)
Traceback (most recent call last):
File ’<stdin>’, line 1, in ?
NameError: name ’l’ is not defined
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 7 / 28
43. Python, the language Functions
The def statement
def function-name(mandatory, optional=expression):
statement(s)
Formal parameters can be
Mandatory Each call must supply a value for the parameter
Optional If the call does not supply a value, the default is used
The def statement evaluates the expression and saves a reference to
the expression value (the default value of the parameter) among the
attributes of the function object
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 8 / 28
44. Python, the language Functions
The def statement
def function-name(mandatory, optional=expression):
statement(s)
Formal parameters can be
Mandatory Each call must supply a value for the parameter
Optional If the call does not supply a value, the default is used
The def statement evaluates the expression and saves a reference to
the expression value (the default value of the parameter) among the
attributes of the function object
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 8 / 28
45. Python, the language Functions
The def statement
def function-name(mandatory, optional=expression):
statement(s)
Formal parameters can be
Mandatory Each call must supply a value for the parameter
Optional If the call does not supply a value, the default is used
The def statement evaluates the expression and saves a reference to
the expression value (the default value of the parameter) among the
attributes of the function object
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 8 / 28
46. Python, the language Functions
The def statement
def function-name(mandatory, optional=expression):
statement(s)
Formal parameters can be
Mandatory Each call must supply a value for the parameter
Optional If the call does not supply a value, the default is used
The def statement evaluates the expression and saves a reference to
the expression value (the default value of the parameter) among the
attributes of the function object
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 8 / 28
47. Python, the language Functions
The def statement
def function-name(mandatory, optional=expression):
statement(s)
Formal parameters can be
Mandatory Each call must supply a value for the parameter
Optional If the call does not supply a value, the default is used
The def statement evaluates the expression and saves a reference to
the expression value (the default value of the parameter) among the
attributes of the function object
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 8 / 28
48. Python, the language Functions
Evaluation of the defaults
The default expression is not evaluated when the function gets called.
>>> def f(x, y=None):
>>> def f(x, y=[]):
... if y is None: y = []
... y.append(x)
... y.append(x)
... return y
... return y
...
...
>>> print f(23)
>>> print f(23)
[23]
[23]
>>> print f(42)
>>> print f(42)
[23, 42]
[42]
The y=None idiom is the standard way to deal with mutable defaults.
Problem: Do you find another solution in this case?
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 9 / 28
49. Python, the language Functions
Evaluation of the defaults
The default expression is not evaluated when the function gets called.
>>> def f(x, y=None):
>>> def f(x, y=[]):
... if y is None: y = []
... y.append(x)
... y.append(x)
... return y
... return y
...
...
>>> print f(23)
>>> print f(23)
[23]
[23]
>>> print f(42)
>>> print f(42)
[23, 42]
[42]
The y=None idiom is the standard way to deal with mutable defaults.
Problem: Do you find another solution in this case?
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 9 / 28
50. Python, the language Functions
Evaluation of the defaults
The default expression is not evaluated when the function gets called.
>>> def f(x, y=None):
>>> def f(x, y=[]):
... if y is None: y = []
... y.append(x)
... y.append(x)
... return y
... return y
...
...
>>> print f(23)
>>> print f(23)
[23]
[23]
>>> print f(42)
>>> print f(42)
[23, 42]
[42]
The y=None idiom is the standard way to deal with mutable defaults.
Problem: Do you find another solution in this case?
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 9 / 28
51. Python, the language Functions
Evaluation of the defaults
The default expression is not evaluated when the function gets called.
>>> def f(x, y=None):
>>> def f(x, y=[]):
... if y is None: y = []
... y.append(x)
... y.append(x)
... return y
... return y
...
...
>>> print f(23)
>>> print f(23)
[23]
[23]
>>> print f(42)
>>> print f(42)
[23, 42]
[42]
The y=None idiom is the standard way to deal with mutable defaults.
Problem: Do you find another solution in this case?
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 9 / 28
52. Python, the language Functions
Evaluation of the defaults
The default expression is not evaluated when the function gets called.
>>> def f(x, y=None):
>>> def f(x, y=[]):
... if y is None: y = []
... y.append(x)
... y.append(x)
... return y
... return y
...
...
>>> print f(23)
>>> print f(23)
[23]
[23]
>>> print f(42)
>>> print f(42)
[23, 42]
[42]
The y=None idiom is the standard way to deal with mutable defaults.
Problem: Do you find another solution in this case?
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 9 / 28
53. Python, the language Functions
Evaluation of the defaults
The default expression is not evaluated when the function gets called.
>>> def f(x, y=None):
>>> def f(x, y=[]):
... if y is None: y = []
... y.append(x)
... y.append(x)
... return y
... return y
...
...
>>> print f(23)
>>> print f(23)
[23]
[23]
>>> print f(42)
>>> print f(42)
[23, 42]
[42]
The y=None idiom is the standard way to deal with mutable defaults.
Problem: Do you find another solution in this case?
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 9 / 28
54. Python, the language Functions
Extra Positional Arguments
At the end of the arguments list you may optionally add the special
form *args
This binds args to a tuple containing the extra values passed in the
function call
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 10 / 28
55. Python, the language Functions
Extra Positional Arguments
At the end of the arguments list you may optionally add the special
form *args
This binds args to a tuple containing the extra values passed in the
function call
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 10 / 28
56. Python, the language Functions
Extra Positional Arguments
At the end of the arguments list you may optionally add the special
form *args
This binds args to a tuple containing the extra values passed in the
function call
>>> def spam(a, b, *args):
... print a, b, args
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 10 / 28
57. Python, the language Functions
Extra Positional Arguments
At the end of the arguments list you may optionally add the special
form *args
This binds args to a tuple containing the extra values passed in the
function call
>>> def spam(a, b, *args):
... print a, b, args
>>> spam(1, 2)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 10 / 28
58. Python, the language Functions
Extra Positional Arguments
At the end of the arguments list you may optionally add the special
form *args
This binds args to a tuple containing the extra values passed in the
function call
>>> def spam(a, b, *args):
... print a, b, args
>>> spam(1, 2)
1 2 ()
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 10 / 28
59. Python, the language Functions
Extra Positional Arguments
At the end of the arguments list you may optionally add the special
form *args
This binds args to a tuple containing the extra values passed in the
function call
>>> def spam(a, b, *args):
... print a, b, args
>>> spam(1, 2)
1 2 ()
>>> spam(b=1, a=2)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 10 / 28
60. Python, the language Functions
Extra Positional Arguments
At the end of the arguments list you may optionally add the special
form *args
This binds args to a tuple containing the extra values passed in the
function call
>>> def spam(a, b, *args):
... print a, b, args
>>> spam(1, 2)
1 2 ()
>>> spam(b=1, a=2)
2 1 ()
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 10 / 28
61. Python, the language Functions
Extra Positional Arguments
At the end of the arguments list you may optionally add the special
form *args
This binds args to a tuple containing the extra values passed in the
function call
>>> def spam(a, b, *args):
... print a, b, args
>>> spam(1, 2)
1 2 ()
>>> spam(b=1, a=2)
2 1 ()
>>> spam(1, 2, 3, 4, 5)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 10 / 28
62. Python, the language Functions
Extra Positional Arguments
At the end of the arguments list you may optionally add the special
form *args
This binds args to a tuple containing the extra values passed in the
function call
>>> def spam(a, b, *args):
... print a, b, args
>>> spam(1, 2)
1 2 ()
>>> spam(b=1, a=2)
2 1 ()
>>> spam(1, 2, 3, 4, 5)
1 2 (3, 4, 5)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 10 / 28
63. Python, the language Functions
Extra Positional Arguments
At the end of the arguments list you may optionally add the special
form *args
This binds args to a tuple containing the extra values passed in the
function call
>>> def spam(a, b, *args): >>> def eggs(a, *args):
... print a, b, args ... spam(a, args)
>>> spam(1, 2)
1 2 ()
>>> spam(b=1, a=2)
2 1 ()
>>> spam(1, 2, 3, 4, 5)
1 2 (3, 4, 5)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 10 / 28
64. Python, the language Functions
Extra Positional Arguments
At the end of the arguments list you may optionally add the special
form *args
This binds args to a tuple containing the extra values passed in the
function call
>>> def spam(a, b, *args): >>> def eggs(a, *args):
... print a, b, args ... spam(a, args)
>>> spam(1, 2) >>> eggs(1, 2, 3, 4, 5)
1 2 ()
>>> spam(b=1, a=2)
2 1 ()
>>> spam(1, 2, 3, 4, 5)
1 2 (3, 4, 5)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 10 / 28
65. Python, the language Functions
Extra Positional Arguments
At the end of the arguments list you may optionally add the special
form *args
This binds args to a tuple containing the extra values passed in the
function call
>>> def spam(a, b, *args): >>> def eggs(a, *args):
... print a, b, args ... spam(a, args)
>>> spam(1, 2) >>> eggs(1, 2, 3, 4, 5)
1 2 () 1 (2, 3, 4, 5) ()
>>> spam(b=1, a=2)
2 1 ()
>>> spam(1, 2, 3, 4, 5)
1 2 (3, 4, 5)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 10 / 28
66. Python, the language Functions
Extra Positional Arguments
At the end of the arguments list you may optionally add the special
form *args
This binds args to a tuple containing the extra values passed in the
function call
>>> def spam(a, b, *args): >>> def eggs(a, *args):
... print a, b, args ... spam(a, args)
>>> spam(1, 2) >>> eggs(1, 2, 3, 4, 5)
1 2 () 1 (2, 3, 4, 5) ()
>>> spam(b=1, a=2) >>> def ham(a, *args):
2 1 () ... spam(a, *args)
>>> spam(1, 2, 3, 4, 5)
1 2 (3, 4, 5)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 10 / 28
67. Python, the language Functions
Extra Positional Arguments
At the end of the arguments list you may optionally add the special
form *args
This binds args to a tuple containing the extra values passed in the
function call
>>> def spam(a, b, *args): >>> def eggs(a, *args):
... print a, b, args ... spam(a, args)
>>> spam(1, 2) >>> eggs(1, 2, 3, 4, 5)
1 2 () 1 (2, 3, 4, 5) ()
>>> spam(b=1, a=2) >>> def ham(a, *args):
2 1 () ... spam(a, *args)
>>> spam(1, 2, 3, 4, 5) >>> ham(1, 2, 3, 4, 5)
1 2 (3, 4, 5)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 10 / 28
68. Python, the language Functions
Extra Positional Arguments
At the end of the arguments list you may optionally add the special
form *args
This binds args to a tuple containing the extra values passed in the
function call
>>> def spam(a, b, *args): >>> def eggs(a, *args):
... print a, b, args ... spam(a, args)
>>> spam(1, 2) >>> eggs(1, 2, 3, 4, 5)
1 2 () 1 (2, 3, 4, 5) ()
>>> spam(b=1, a=2) >>> def ham(a, *args):
2 1 () ... spam(a, *args)
>>> spam(1, 2, 3, 4, 5) >>> ham(1, 2, 3, 4, 5)
1 2 (3, 4, 5) 1 2 (3, 4, 5)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 10 / 28
69. Python, the language Functions
Extra Named Arguments
At the end of the arguments list you may optionally add the special
from **kwargs
This binds kwargs to a dictionary containing the extra values passed
in the function call and their names
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 11 / 28
70. Python, the language Functions
Extra Named Arguments
At the end of the arguments list you may optionally add the special
from **kwargs
This binds kwargs to a dictionary containing the extra values passed
in the function call and their names
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 11 / 28
71. Python, the language Functions
Extra Named Arguments
At the end of the arguments list you may optionally add the special
from **kwargs
This binds kwargs to a dictionary containing the extra values passed
in the function call and their names
>>> def spam(a, b, **kw):
... print a, b, kw
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 11 / 28
72. Python, the language Functions
Extra Named Arguments
At the end of the arguments list you may optionally add the special
from **kwargs
This binds kwargs to a dictionary containing the extra values passed
in the function call and their names
>>> def spam(a, b, **kw):
... print a, b, kw
>>> spam(1, 2)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 11 / 28
73. Python, the language Functions
Extra Named Arguments
At the end of the arguments list you may optionally add the special
from **kwargs
This binds kwargs to a dictionary containing the extra values passed
in the function call and their names
>>> def spam(a, b, **kw):
... print a, b, kw
>>> spam(1, 2)
1 2 {}
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 11 / 28
74. Python, the language Functions
Extra Named Arguments
At the end of the arguments list you may optionally add the special
from **kwargs
This binds kwargs to a dictionary containing the extra values passed
in the function call and their names
>>> def spam(a, b, **kw):
... print a, b, kw
>>> spam(1, 2)
1 2 {}
>>> spam(b=1, a=2, c=3, d=4)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 11 / 28
75. Python, the language Functions
Extra Named Arguments
At the end of the arguments list you may optionally add the special
from **kwargs
This binds kwargs to a dictionary containing the extra values passed
in the function call and their names
>>> def spam(a, b, **kw):
... print a, b, kw
>>> spam(1, 2)
1 2 {}
>>> spam(b=1, a=2, c=3, d=4)
2 1 {’c’: 3, ’d’: 4}
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 11 / 28
76. Python, the language Functions
Extra Named Arguments
At the end of the arguments list you may optionally add the special
from **kwargs
This binds kwargs to a dictionary containing the extra values passed
in the function call and their names
>>> def spam(a, b, **kw):
... print a, b, kw
>>> spam(1, 2)
1 2 {}
>>> spam(b=1, a=2, c=3, d=4)
2 1 {’c’: 3, ’d’: 4}
>>> def eggs(a, *args, **kw):
... print a, args, kw
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 11 / 28
77. Python, the language Functions
Extra Named Arguments
At the end of the arguments list you may optionally add the special
from **kwargs
This binds kwargs to a dictionary containing the extra values passed
in the function call and their names
>>> def spam(a, b, **kw):
... print a, b, kw
>>> spam(1, 2)
1 2 {}
>>> spam(b=1, a=2, c=3, d=4)
2 1 {’c’: 3, ’d’: 4}
>>> def eggs(a, *args, **kw):
... print a, args, kw
>>> eggs(1,2,3,4)
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 11 / 28
78. Python, the language Functions
Extra Named Arguments
At the end of the arguments list you may optionally add the special
from **kwargs
This binds kwargs to a dictionary containing the extra values passed
in the function call and their names
>>> def spam(a, b, **kw):
... print a, b, kw
>>> spam(1, 2)
1 2 {}
>>> spam(b=1, a=2, c=3, d=4)
2 1 {’c’: 3, ’d’: 4}
>>> def eggs(a, *args, **kw):
... print a, args, kw
>>> eggs(1,2,3,4)
1 (2, 3, 4) {}
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 11 / 28
79. Python, the language Functions
Extra Named Arguments
At the end of the arguments list you may optionally add the special
from **kwargs
This binds kwargs to a dictionary containing the extra values passed
in the function call and their names
>>> def spam(a, b, **kw): >>> def ham(a,**kw):
... print a, b, kw ... spam(a, kw)
>>> spam(1, 2)
1 2 {}
>>> spam(b=1, a=2, c=3, d=4)
2 1 {’c’: 3, ’d’: 4}
>>> def eggs(a, *args, **kw):
... print a, args, kw
>>> eggs(1,2,3,4)
1 (2, 3, 4) {}
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 11 / 28
80. Python, the language Functions
Extra Named Arguments
At the end of the arguments list you may optionally add the special
from **kwargs
This binds kwargs to a dictionary containing the extra values passed
in the function call and their names
>>> def spam(a, b, **kw): >>> def ham(a,**kw):
... print a, b, kw ... spam(a, kw)
>>> spam(1, 2) >>> ham(1, b=2, c=3)
1 2 {}
>>> spam(b=1, a=2, c=3, d=4)
2 1 {’c’: 3, ’d’: 4}
>>> def eggs(a, *args, **kw):
... print a, args, kw
>>> eggs(1,2,3,4)
1 (2, 3, 4) {}
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 11 / 28
81. Python, the language Functions
Extra Named Arguments
At the end of the arguments list you may optionally add the special
from **kwargs
This binds kwargs to a dictionary containing the extra values passed
in the function call and their names
>>> def spam(a, b, **kw): >>> def ham(a,**kw):
... print a, b, kw ... spam(a, kw)
>>> spam(1, 2) >>> ham(1, b=2, c=3)
1 2 {} 1 {’c’: 3, ’b’: 2} {}
>>> spam(b=1, a=2, c=3, d=4)
2 1 {’c’: 3, ’d’: 4}
>>> def eggs(a, *args, **kw):
... print a, args, kw
>>> eggs(1,2,3,4)
1 (2, 3, 4) {}
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 11 / 28
82. Python, the language Functions
Extra Named Arguments
At the end of the arguments list you may optionally add the special
from **kwargs
This binds kwargs to a dictionary containing the extra values passed
in the function call and their names
>>> def spam(a, b, **kw): >>> def ham(a,**kw):
... print a, b, kw ... spam(a, kw)
>>> spam(1, 2) >>> ham(1, b=2, c=3)
1 2 {} 1 {’c’: 3, ’b’: 2} {}
>>> spam(b=1, a=2, c=3, d=4) >>> def pram(a,**kw):
2 1 {’c’: 3, ’d’: 4} ... spam(a, **kw)
>>> def eggs(a, *args, **kw):
... print a, args, kw
>>> eggs(1,2,3,4)
1 (2, 3, 4) {}
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 11 / 28
83. Python, the language Functions
Extra Named Arguments
At the end of the arguments list you may optionally add the special
from **kwargs
This binds kwargs to a dictionary containing the extra values passed
in the function call and their names
>>> def spam(a, b, **kw): >>> def ham(a,**kw):
... print a, b, kw ... spam(a, kw)
>>> spam(1, 2) >>> ham(1, b=2, c=3)
1 2 {} 1 {’c’: 3, ’b’: 2} {}
>>> spam(b=1, a=2, c=3, d=4) >>> def pram(a,**kw):
2 1 {’c’: 3, ’d’: 4} ... spam(a, **kw)
>>> def eggs(a, *args, **kw): >>> pram(1, b=2, c=3)
... print a, args, kw
>>> eggs(1,2,3,4)
1 (2, 3, 4) {}
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 11 / 28
84. Python, the language Functions
Extra Named Arguments
At the end of the arguments list you may optionally add the special
from **kwargs
This binds kwargs to a dictionary containing the extra values passed
in the function call and their names
>>> def spam(a, b, **kw): >>> def ham(a,**kw):
... print a, b, kw ... spam(a, kw)
>>> spam(1, 2) >>> ham(1, b=2, c=3)
1 2 {} 1 {’c’: 3, ’b’: 2} {}
>>> spam(b=1, a=2, c=3, d=4) >>> def pram(a,**kw):
2 1 {’c’: 3, ’d’: 4} ... spam(a, **kw)
>>> def eggs(a, *args, **kw): >>> pram(1, b=2, c=3)
... print a, args, kw 1 2 {’c’: 3}
>>> eggs(1,2,3,4)
1 (2, 3, 4) {}
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 11 / 28
85. Python, the language Modules and Packages
Modules
A typical Python program is made up of several source files
Each source file corresponds to a module that groups variables,
functions, classes, etc. for reuse
A module explicitly establishes its dependencies using the import and
from statements
In some languages global variables provide a hidden mechanism for
coupling between modules
In Python global variables are not global to all modules: they are
attributes of a module object (module.variable)
Extensions, components coded in other languages such as C, C++,
Java, C#, are treated as modules by the python code
In Python everything is defined in a module:
main program or interactive sessions in module main
built-ins in preloaded module builtin accessible via import
builtin
at loading, modules get an extra attribute named builtins which
refers to either to module builtin or to its dictionary
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 12 / 28
86. Python, the language Modules and Packages
Modules
A typical Python program is made up of several source files
Each source file corresponds to a module that groups variables,
functions, classes, etc. for reuse
A module explicitly establishes its dependencies using the import and
from statements
In some languages global variables provide a hidden mechanism for
coupling between modules
In Python global variables are not global to all modules: they are
attributes of a module object (module.variable)
Extensions, components coded in other languages such as C, C++,
Java, C#, are treated as modules by the python code
In Python everything is defined in a module:
main program or interactive sessions in module main
built-ins in preloaded module builtin accessible via import
builtin
at loading, modules get an extra attribute named builtins which
refers to either to module builtin or to its dictionary
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 12 / 28
87. Python, the language Modules and Packages
Modules
A typical Python program is made up of several source files
Each source file corresponds to a module that groups variables,
functions, classes, etc. for reuse
A module explicitly establishes its dependencies using the import and
from statements
In some languages global variables provide a hidden mechanism for
coupling between modules
In Python global variables are not global to all modules: they are
attributes of a module object (module.variable)
Extensions, components coded in other languages such as C, C++,
Java, C#, are treated as modules by the python code
In Python everything is defined in a module:
main program or interactive sessions in module main
built-ins in preloaded module builtin accessible via import
builtin
at loading, modules get an extra attribute named builtins which
refers to either to module builtin or to its dictionary
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 12 / 28
88. Python, the language Modules and Packages
Modules
A typical Python program is made up of several source files
Each source file corresponds to a module that groups variables,
functions, classes, etc. for reuse
A module explicitly establishes its dependencies using the import and
from statements
In some languages global variables provide a hidden mechanism for
coupling between modules
In Python global variables are not global to all modules: they are
attributes of a module object (module.variable)
Extensions, components coded in other languages such as C, C++,
Java, C#, are treated as modules by the python code
In Python everything is defined in a module:
main program or interactive sessions in module main
built-ins in preloaded module builtin accessible via import
builtin
at loading, modules get an extra attribute named builtins which
refers to either to module builtin or to its dictionary
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 12 / 28
89. Python, the language Modules and Packages
Modules
A typical Python program is made up of several source files
Each source file corresponds to a module that groups variables,
functions, classes, etc. for reuse
A module explicitly establishes its dependencies using the import and
from statements
In some languages global variables provide a hidden mechanism for
coupling between modules
In Python global variables are not global to all modules: they are
attributes of a module object (module.variable)
Extensions, components coded in other languages such as C, C++,
Java, C#, are treated as modules by the python code
In Python everything is defined in a module:
main program or interactive sessions in module main
built-ins in preloaded module builtin accessible via import
builtin
at loading, modules get an extra attribute named builtins which
refers to either to module builtin or to its dictionary
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 12 / 28
90. Python, the language Modules and Packages
Modules
A typical Python program is made up of several source files
Each source file corresponds to a module that groups variables,
functions, classes, etc. for reuse
A module explicitly establishes its dependencies using the import and
from statements
In some languages global variables provide a hidden mechanism for
coupling between modules
In Python global variables are not global to all modules: they are
attributes of a module object (module.variable)
Extensions, components coded in other languages such as C, C++,
Java, C#, are treated as modules by the python code
In Python everything is defined in a module:
main program or interactive sessions in module main
built-ins in preloaded module builtin accessible via import
builtin
at loading, modules get an extra attribute named builtins which
refers to either to module builtin or to its dictionary
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 12 / 28
91. Python, the language Modules and Packages
Modules
A typical Python program is made up of several source files
Each source file corresponds to a module that groups variables,
functions, classes, etc. for reuse
A module explicitly establishes its dependencies using the import and
from statements
In some languages global variables provide a hidden mechanism for
coupling between modules
In Python global variables are not global to all modules: they are
attributes of a module object (module.variable)
Extensions, components coded in other languages such as C, C++,
Java, C#, are treated as modules by the python code
In Python everything is defined in a module:
main program or interactive sessions in module main
built-ins in preloaded module builtin accessible via import
builtin
at loading, modules get an extra attribute named builtins which
refers to either to module builtin or to its dictionary
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 12 / 28
92. Python, the language Modules and Packages
Modules
A typical Python program is made up of several source files
Each source file corresponds to a module that groups variables,
functions, classes, etc. for reuse
A module explicitly establishes its dependencies using the import and
from statements
In some languages global variables provide a hidden mechanism for
coupling between modules
In Python global variables are not global to all modules: they are
attributes of a module object (module.variable)
Extensions, components coded in other languages such as C, C++,
Java, C#, are treated as modules by the python code
In Python everything is defined in a module:
main program or interactive sessions in module main
built-ins in preloaded module builtin accessible via import
builtin
at loading, modules get an extra attribute named builtins which
refers to either to module builtin or to its dictionary
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 12 / 28
93. Python, the language Modules and Packages
Modules
A typical Python program is made up of several source files
Each source file corresponds to a module that groups variables,
functions, classes, etc. for reuse
A module explicitly establishes its dependencies using the import and
from statements
In some languages global variables provide a hidden mechanism for
coupling between modules
In Python global variables are not global to all modules: they are
attributes of a module object (module.variable)
Extensions, components coded in other languages such as C, C++,
Java, C#, are treated as modules by the python code
In Python everything is defined in a module:
main program or interactive sessions in module main
built-ins in preloaded module builtin accessible via import
builtin
at loading, modules get an extra attribute named builtins which
refers to either to module builtin or to its dictionary
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 12 / 28
94. Python, the language Modules and Packages
Modules
A typical Python program is made up of several source files
Each source file corresponds to a module that groups variables,
functions, classes, etc. for reuse
A module explicitly establishes its dependencies using the import and
from statements
In some languages global variables provide a hidden mechanism for
coupling between modules
In Python global variables are not global to all modules: they are
attributes of a module object (module.variable)
Extensions, components coded in other languages such as C, C++,
Java, C#, are treated as modules by the python code
In Python everything is defined in a module:
main program or interactive sessions in module main
built-ins in preloaded module builtin accessible via import
builtin
at loading, modules get an extra attribute named builtins which
refers to either to module builtin or to its dictionary
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 12 / 28
95. Python, the language Modules and Packages
import modulename
spam.py
#!/usr/bin/env python
# -*- coding: latin-1 -*-
quot;quot;quot;Documentation of the
spam module.quot;quot;quot;
def eggs():
quot;quot;quot;eggs documentationquot;quot;quot;
print quot;eggs !!!quot;
def spam(s):
quot;quot;quot;spam documentationquot;quot;quot;
print quot;spam !!!quot;
if __name__ == quot;__main__quot;:
eggs()
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 13 / 28
96. Python, the language Modules and Packages
import modulename
>>> import spam
spam.py
#!/usr/bin/env python
# -*- coding: latin-1 -*-
quot;quot;quot;Documentation of the
spam module.quot;quot;quot;
def eggs():
quot;quot;quot;eggs documentationquot;quot;quot;
print quot;eggs !!!quot;
def spam(s):
quot;quot;quot;spam documentationquot;quot;quot;
print quot;spam !!!quot;
if __name__ == quot;__main__quot;:
eggs()
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 13 / 28
97. Python, the language Modules and Packages
import modulename
>>> import spam
spam.py >>> spam
#!/usr/bin/env python
# -*- coding: latin-1 -*-
quot;quot;quot;Documentation of the
spam module.quot;quot;quot;
def eggs():
quot;quot;quot;eggs documentationquot;quot;quot;
print quot;eggs !!!quot;
def spam(s):
quot;quot;quot;spam documentationquot;quot;quot;
print quot;spam !!!quot;
if __name__ == quot;__main__quot;:
eggs()
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 13 / 28
98. Python, the language Modules and Packages
import modulename
>>> import spam
spam.py >>> spam
#!/usr/bin/env python <module ’spam’ from ’spam.pyc’>
# -*- coding: latin-1 -*-
quot;quot;quot;Documentation of the
spam module.quot;quot;quot;
def eggs():
quot;quot;quot;eggs documentationquot;quot;quot;
print quot;eggs !!!quot;
def spam(s):
quot;quot;quot;spam documentationquot;quot;quot;
print quot;spam !!!quot;
if __name__ == quot;__main__quot;:
eggs()
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 13 / 28
99. Python, the language Modules and Packages
import modulename
>>> import spam
spam.py >>> spam
#!/usr/bin/env python <module ’spam’ from ’spam.pyc’>
# -*- coding: latin-1 -*- >>> eggs()
quot;quot;quot;Documentation of the
spam module.quot;quot;quot;
def eggs():
quot;quot;quot;eggs documentationquot;quot;quot;
print quot;eggs !!!quot;
def spam(s):
quot;quot;quot;spam documentationquot;quot;quot;
print quot;spam !!!quot;
if __name__ == quot;__main__quot;:
eggs()
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 13 / 28
100. Python, the language Modules and Packages
import modulename
>>> import spam
spam.py >>> spam
#!/usr/bin/env python <module ’spam’ from ’spam.pyc’>
# -*- coding: latin-1 -*- >>> eggs()
quot;quot;quot;Documentation of the Traceback (most recent call last):
spam module.quot;quot;quot; File ’<stdin>’, line 1, in ?
NameError: name ’eggs’ is not defined
def eggs():
quot;quot;quot;eggs documentationquot;quot;quot;
print quot;eggs !!!quot;
def spam(s):
quot;quot;quot;spam documentationquot;quot;quot;
print quot;spam !!!quot;
if __name__ == quot;__main__quot;:
eggs()
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 13 / 28
101. Python, the language Modules and Packages
import modulename
>>> import spam
spam.py >>> spam
#!/usr/bin/env python <module ’spam’ from ’spam.pyc’>
# -*- coding: latin-1 -*- >>> eggs()
quot;quot;quot;Documentation of the Traceback (most recent call last):
spam module.quot;quot;quot; File ’<stdin>’, line 1, in ?
NameError: name ’eggs’ is not defined
def eggs(): >>> spam.eggs()
quot;quot;quot;eggs documentationquot;quot;quot;
print quot;eggs !!!quot;
def spam(s):
quot;quot;quot;spam documentationquot;quot;quot;
print quot;spam !!!quot;
if __name__ == quot;__main__quot;:
eggs()
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 13 / 28
102. Python, the language Modules and Packages
import modulename
>>> import spam
spam.py >>> spam
#!/usr/bin/env python <module ’spam’ from ’spam.pyc’>
# -*- coding: latin-1 -*- >>> eggs()
quot;quot;quot;Documentation of the Traceback (most recent call last):
spam module.quot;quot;quot; File ’<stdin>’, line 1, in ?
NameError: name ’eggs’ is not defined
def eggs(): >>> spam.eggs()
quot;quot;quot;eggs documentationquot;quot;quot; eggs !!!
print quot;eggs !!!quot;
def spam(s):
quot;quot;quot;spam documentationquot;quot;quot;
print quot;spam !!!quot;
if __name__ == quot;__main__quot;:
eggs()
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 13 / 28
103. Python, the language Modules and Packages
import modulename
>>> import spam
spam.py >>> spam
#!/usr/bin/env python <module ’spam’ from ’spam.pyc’>
# -*- coding: latin-1 -*- >>> eggs()
quot;quot;quot;Documentation of the Traceback (most recent call last):
spam module.quot;quot;quot; File ’<stdin>’, line 1, in ?
NameError: name ’eggs’ is not defined
def eggs(): >>> spam.eggs()
quot;quot;quot;eggs documentationquot;quot;quot; eggs !!!
print quot;eggs !!!quot; >>> ham=spam
def spam(s):
quot;quot;quot;spam documentationquot;quot;quot;
print quot;spam !!!quot;
if __name__ == quot;__main__quot;:
eggs()
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 13 / 28
104. Python, the language Modules and Packages
import modulename
>>> import spam
spam.py >>> spam
#!/usr/bin/env python <module ’spam’ from ’spam.pyc’>
# -*- coding: latin-1 -*- >>> eggs()
quot;quot;quot;Documentation of the Traceback (most recent call last):
spam module.quot;quot;quot; File ’<stdin>’, line 1, in ?
NameError: name ’eggs’ is not defined
def eggs(): >>> spam.eggs()
quot;quot;quot;eggs documentationquot;quot;quot; eggs !!!
print quot;eggs !!!quot; >>> ham=spam
>>> ham.spam()
def spam(s):
quot;quot;quot;spam documentationquot;quot;quot;
print quot;spam !!!quot;
if __name__ == quot;__main__quot;:
eggs()
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 13 / 28
105. Python, the language Modules and Packages
import modulename
>>> import spam
spam.py >>> spam
#!/usr/bin/env python <module ’spam’ from ’spam.pyc’>
# -*- coding: latin-1 -*- >>> eggs()
quot;quot;quot;Documentation of the Traceback (most recent call last):
spam module.quot;quot;quot; File ’<stdin>’, line 1, in ?
NameError: name ’eggs’ is not defined
def eggs(): >>> spam.eggs()
quot;quot;quot;eggs documentationquot;quot;quot; eggs !!!
print quot;eggs !!!quot; >>> ham=spam
>>> ham.spam()
def spam(s): spam !!!
quot;quot;quot;spam documentationquot;quot;quot;
print quot;spam !!!quot;
if __name__ == quot;__main__quot;:
eggs()
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 13 / 28
106. Python, the language Modules and Packages
import modulename
>>> import spam
spam.py >>> spam
#!/usr/bin/env python <module ’spam’ from ’spam.pyc’>
# -*- coding: latin-1 -*- >>> eggs()
quot;quot;quot;Documentation of the Traceback (most recent call last):
spam module.quot;quot;quot; File ’<stdin>’, line 1, in ?
NameError: name ’eggs’ is not defined
def eggs(): >>> spam.eggs()
quot;quot;quot;eggs documentationquot;quot;quot; eggs !!!
print quot;eggs !!!quot; >>> ham=spam
>>> ham.spam()
def spam(s): spam !!!
quot;quot;quot;spam documentationquot;quot;quot; >>> dir()
print quot;spam !!!quot;
if __name__ == quot;__main__quot;:
eggs()
J.M.Gimeno (jmgimeno@diei.udl.cat) Python: the Project, ... October 2008 13 / 28