时间不够直接跳过前边一些东西,从9.3开始
9.3. A First Look at Classes
Classes introduce a little bit of new syntax, three new object types, and some new semantics.
9.3.1. Class Definition Syntax
类语法,下面是一个很简单的class的定义。
The simplest form of class definition looks like this:
Class definitions, like function definitions (def
In practice, the statements inside a class definition will usually be function definitions, but other statements are allowed, and sometimes useful — we’ll come back to this later. The function definitions inside a class normally have a peculiar form of argument list, dictated by the calling conventions for methods — again, this is explained later.
When a class definition is entered, a new namespace is created, and used as the local scope — thus, all assignments to local variables go into this new namespace. In particular, function definitions bind the name of the new function here.
When a class definition is left normally (via the end), a
9.3.2. Class Objects
类对象,支持两种操作,attribute references 和instantiation
Class objects support two kinds of operations: attribute references and instantiation.
Attribute references
attribute references 是标准的语法,可以这样引用:obj.name
then
Class
第二种方法instantiation要用function来标记才行。调用函数以后会返回一个实例。可以像下面一样调用了
creates a new
The instantiation operation (“calling” a class object) creates an empty object. Many classes like to create objects with instances customized to a specific initial state. Therefore a class may define a special method named__init__(), like this:
When a class defines an
Of course, the
9.5. Inheritance
Of course, a language feature would not be worthy of the name “class” without supporting inheritance. The syntax for a derived class definition looks like this:
一个没有继承的class就不能称为class
The name
基类的名字就在上面的例子中定义,代替掉基类的名称,其他表达式也是被允许的。下面看看例子
Execution of a derived class definition proceeds the same as for a base class. When the class object is constructed, the base class is remembered. This is used for resolving attribute references: if a requested attribute is not found in the class, the search proceeds to look in the base class. This rule is applied recursively if the base class itself is derived from some other class.
There’s nothing special about instantiation of derived classes:
Derived classes may override methods of their base classes. Because methods have no special privileges when calling other methods of the same object, a method of a base class that calls another method defined in the same base class may end up calling a method of a derived class that overrides it. (For C++ programmers: all methods in Python are effectively
An overriding method in a derived class may in fact want to extend rather than simply replace the base class method of the same name. There is a simple way to call the base class method directly: just call
Python has two built-in functions that work with inheritance:
- Use
isinstance() to check an instance’s type: isinstance(obj, int) will be True only if obj.__class__ is int or some class derived from int. - Use
issubclass() to check class inheritance: issubclass(bool, int) is True since bool is a subclass of int. However,issubclass(unicode, str) is False since unicode is not a subclass of str (they only share a common ancestor,basestring).
9.6. Private Variables
private这个实例变量不能随便被修改,除非不纯在一个对象里边。通常用带下划线前缀名称标识非公共API的部分。
“Private” instance variables that cannot be accessed except from inside an object don’t exist in Python. However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g.
Since there is a valid use-case for class-private members (namely to avoid name clashes of names with names defined by subclasses), there is limited support for such a mechanism, called
Name mangling is helpful for letting subclasses override methods without breaking intraclass method calls. For example:
Note that the mangling rules are designed mostly to avoid accidents; it still is possible to access or modify a variable that is considered private. This can even be useful in special circumstances, such as in the debugger.
Notice that code passed to