Python Note Two
Polymorphism: even if you don’t know what kind of object a variable refer to, you may still able to perform operations on it, that will work differently depending on the class(type) of the object
Polymorphism enables you to call the methods of an object without knowing its class (type of object). Encapsulation enables you to use the object without worrying about how it’s constructd.
To make a method or attribute private (inaccessible from outside) simply starts its name with two underscores
Inside a class definition, all names beginning with a double underscores are
translated
by adding a single underscore and the class name to the beginning.Names with an initial underscore are’t imported with starred imports.
issubclass(SpamFilter, Filter)
isinstance(obj, cls)
|
|
obj.__class__
: find out which class an object belongs toIn multiple inheritance, the methods in the earlier classes override the methods in the later ones.
Object attributes
getattr(object, name [, default])
hasattr(object, name)
setattr(object, name, value)
Explore built-in exceptions (in module exceptions)
|
|
- Cataching two exceptions with one block
|
|
- Catching the exception object:
|
|
- example
|
|
If you override the constructor of a class, you need to call the constructor of the superclass or risk having an object that isn’t properly initialized.
If you retrieve the method from the class directly, there is no instance to which to bind, such method is called unbound, you can supply any self you want.
|
|
New style classes,
super
is called with the current class and instance as its arguments. Any method called on the returned object, will be fetched from the superclass. This method returns super object.super(SongBird, self).__init__()
In PY3,
super
can be called without any argument.sequence and mapping protocol
- immutable
__len__(sel))
if return0
treat asFalse
in boolean contxt, can overrite with__nonzero__
__getitem__(self, key)
- mutable
__setitem__(self, key, value)
__delitem__(self, key)
this is called when someone uses thedel
statement on a part of the object
- immutable
For a sequence
x[-n]
is the same asx[len(x) - n]
if the key is of an inappropriate type:TypeError
if the index of a sequence outside the allowed range:IndexError
Examples
|
|
|
|
The attributes that are defined through their accessors are often called properties
Property example
|
|
property(fget, fset, fdel, doc)
returns a property, all arguments are optionalproperty
isn’t really a function, it’s a class whose instances have some methods that do all the work. Those methods defines that so-calleddescriptor
protocol. Eg: if the attribute bound to an object that implements__get__
method, the object won’t simply retuned, instead__get__
method will be called and the resulting value will be returnedstatic methods VS class methods
- static methods are defined without self arguments and they can be called directly on the class itself
- class methods are defined with a self-like parameter normally called
cls
. You can call class methods directly on the class object too, but parameter then automatically is bound to the class.
__getattr__
__setattr__
and friends__getattribute__(self, name)
: Automatically called when the attribute name is called (only on new-style class)__getattr__(self, name)
: Automatically called when the attribute name is called and the object has no such attribute__setattr__(self, name, value)
: Automatically called when an attempt is made to bind the attribute name to value__delattr__(self, name)
Example
|
|
__dict__
contains a dic with all the instance attributesThere is a trap associated with
__getattribute__
, it intercepts all attributre access, and__dict__
as well. The only safe way to access attribute on self inside__getattribute__
is to use the__getattribute__
method of the superclass