Namespace and Objects
Namespaceis a mapping from names to objects including built-in names (e.g. abs()), global names in a module and the local names in a function.
- You can access to any names in a module by
modname.attributeName. - Any attribute in the module is writable. e.g. modname.the_answer = 42.
- You can also delete the attribute from the object. e.g. del modname.the_answer
- The built-in names is created when Python interpreter starts and never deleted.
- The global namesapce of a module is created when the module definition is read in, the module namespace last until the interpreter quits.
- The local namespace of a function is created when the function is called and deleted when the function returns or raises an unhandled exception
Scopeis a textual region of a Python program where a namespace is directly accessible.
- ‘nonlocal’ statement can be used to rebind a variable found outside.
- ‘global’ statement can be used to indicate that paticular variables live in the global scpe and rebould here
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
The output is:
1 2 3 4 | |
A First Look at Class
class definition looks like:
1 2 3 4 5 6 | |
When a class definition is entered, a new namespace is created and used as the local scope.
Class Objects
support two kinds of operations: attribute references and instantiation.
- Attribute references uses the standard syntax used for all attribute references:
obj.name
1 2 3 4 | |
Then MyClass.i and MyClass.f are valid attribute references, returning an integer and a function object.
- class instantiation uses function notation.
1
| |
will create a new instance of the class and assigns this object to variable x.
A class may define a special method __init__() for instantiation.
1 2 | |
The __init__() method may have arguments
1 2 3 4 5 6 7 | |
Instance Objects
Data attributes need not be declared
1 2 3 4 5 6 | |
If instance attribute reference is a method. This method must be declared in the class before
In the above example, x.f is a valid method, but x.i is not.
Method Objects
object.method is a method object, and can be stored and called at a later time.
1 2 3 4 | |
call x.f() is equivalent to MyClass.f(x)
Class and Instance Variables
Instance variables are data unique to each instance. class variables are shared by all instances
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
- shared data can have problems with involing mutable objects such as
listsanddictionaries
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
The correct design should be like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
Random Remarks
- It is ok to assign an outer method into a local variable in a class
1 2 3 4 5 6 7 8 | |
Now h(), g(), f() are all function method of class C
- In the same class, one method may call other methods by using method attributes of the
selfargument:
1 2 3 4 5 6 7 8 | |
Inheritance
- syntax
1 2 | |
BaseClassName must be defined in a scope containing the derived class definition.
or
1
| |
You can use DerivedClassName() to create a new instance.
Derived Classes may override methods of base class. But you can still call the method in baseclass by
BaseClassName.methodname(self, arguments)There are two built-in functions work with inheritance:
- isinstance(obj, int): check if obj is derived from int
- issubclass(bool, int): check if bool is the subclass of int
Multiple Inheritance
1 2 3 4 | |
Private Variables
The private variables in other languages do not exit in Python. Name Mangling is helpful to allow subclasses override methods without breaking intraclass methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
Exceptions
Exceptions are also identified by classes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
The result is D->C->B. If except B is the first, the result is B->B->B. The first matching except is triggered first.
Iterators
Most container objects can be looped over using for statement
1 2 3 4 5 6 7 8 9 10 | |
You can also define an interator and call next()
1 2 3 4 5 6 | |
In you class, you can define
__iter__()method which returns an object with a__next__()method. If the class defines__next__(), then__iter__()can just return self
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
Generators
Generators are written like regular functions but use yield statement when then want to return data.
1 2 3 4 5 6 7 8 9 10 11 | |
- Generator Expressions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |