Python’s
Dynamic Nature
An investigation of how Python’s dynamic nature
allows it to scale up from one-line scripts to massive
applications.
Kiran Jonnalagadda <jace@seacrow.com>
These are rough slides. The finals will make heavy use
of diagrams in place of text.
What Does
Interpreted Mean?
• Python is not line interpreted like shell
script.

• Python is byte-code compiled like Java,
but compilation is on-the-fly.

• However, Python in some ways behaves
like a line-interpreted language, making
the learning curve easy.
Loading a Module
• When a module (file) is loaded, it is first
compiled into byte-code.

• Python then executes all the code in the
module that is not in a function.

• This code can modify the module as it

loads, making for the line-interpreted feel.

• Code in functions is only executed on call.
Example of Loading
# Example code
print “Top level executes on load.”
def somefunc():
print “Executes only when called.”
class foo:
print “Class level also executes on load.”
def bar(self):
print “Executes only when called.”
Bags of Attributes
• Python is a fully object oriented language.
• All symbols are objects with attributes.
• Objects contain other objects. Therefore,
Python’s namespace is nested.

• A module is an object, a string is an

object, an integer is an object, everything
is an object.
__attributes__
• Python inserts several special attributes
into objects. These attributes are all
named in the __attribute__ style.

• Example 1: __name__ indicates the name
given to the object in code.

• Example 2: __class__ refers to the class
this object is an instance of.
Operator Overloading
• Operator overloading is also achieved via
functions with special attribute names.

• Examples:
• __eq__ and __ne__ for == and !=
• __add__ to add two objects
• __cmp__ to compare, for sorting
Controlling Attribute
Access

• An object can take control of the way its
attributes are accessed.

• The __getitem__ function for list or

dictionary access. Example: object[‘foo’]

• The __getattr__ function for named

access. Example: using object.foo will call
object.__getattr__(‘foo’)

• __setitem__ and __setattr__ for editing.
Documentation
• Python code is self-documenting.
• Documentation can be placed in a string,

which must be the first non-remark
declaration in a module, class or function.

• This documentation is accessible as the
__doc__ attribute of the object.

• Example:

class foo:
“This is the documentation.”
pass
Namespaces
• In Python, everything is contained within
something else.

• The top-level, invisible container is called
__builtins__. Contains internal functions.

• Top-level symbols not from __builtins__
are local symbols.

• Local symbols are not private. They can
be accessed via the namespace.
Namespaces #2
• The top-level namespace is not crowded;

an app can have several modules with
conflicting names and not have a conflict.

• “module1.cname” is different from
“module2.cname”

• Using “name = module1.cname” makes

“name” a local alias for “module1.cname”
Symbols and Objects
• Symbols are not the same as objects.
• Symbols are only references to objects.
• An object can have multiple references.
• Objects are garbage collected when they

have zero references. GC algorithms used
are both reference counting and markand-sweep.
Multiple References
• An module can consist of nothing but

references to symbols in other modules.

• Hence the term, “bags of attributes.”
• This allows a large package to have a

single API module that links to the rest of
the package internally.
References Example
module1.py:
def foo(bar, baz):
return bar + baz

module2.py:
import module1
new_name = module1.foo

module3.py:
from module2 import new_name
print new_name(1, 2)
Runtime Editing
• Python code can edit itself at run-time.
• The only way to define variables in a class
is by editing the instance at runtime.

• Editing involves simply changing the
object a symbol refers to.

• This only works when all code refers to
the object via the symbol, which is the
case almost all the time.
How Classes Work
• {To insert code examples and diagram

here, showing a complex class definition
and how it works when different parts are
accessed. This will be across several
slides.}
Scalability
• Because of the clear namespace

separation, a Python application can
contain thousands of modules without
conflicts.

• Advanced class definition ability helps
avoid repetitive code.

• This makes Python code easy to maintain
and scale, while still remaining simple.
The Type/Class
Dichotomy

• Prior to version 2.2, Python had two
types of objects: Types and Classes.

• Types could only be defined in C code.
• Classes could only be defined in Python.
• Classes could not be derived from Types.
• This meant a base-class could not be
defined externally in C/C++ code.
Zope’s Solution
• Jim Fulton, the creator of Zope, created a
new Type called ExtensionClass.

• ExtensionClass can be derived from, like
a regular Python Class, but is a Type.

• Achieved by patching Python internals.
• ExtensionClass made a lot of extensions

possible that could not be done in Python.
Type/Class Merger
• Python 2.2 merges Types and Classes, so
they are all one type of object.

• But changes are incompatible with

ExtensionClass, which Zope depends on.

• So Zope still uses the old Type/Class

divide, even with Python 2.2 and 2.3.

• Zope will upgrade with Zope 3.