The Python Tutorial 9. Classes
来源:互联网 发布:外国直播软件 编辑:程序博客网 时间:2024/05/16 18:06
时间不够直接跳过前边一些东西,从9.3开始
9.3. A First Look at Classes
Classes introducea little bit of new syntax, three new object types, and some newsemantics.
9.3.1. Class Definition Syntax
类语法,下面是一个很简单的class的定义。
The simplest formof class definition looks like this:
Classdefinitions, like function definitions (def
In practice, thestatements inside a class definition will usually be functiondefinitions, but other statements are allowed, and sometimes useful— we’ll come back to this later. The function definitions inside aclass normally have a peculiar form of argument list, dictated bythe calling conventions for methods — again, this is explainedlater.
When a classdefinition is entered, a new namespace is created, and used as thelocal scope — thus, all assignments to local variables go into thisnew namespace. In particular, function definitions bind the name ofthe new function here.
When a classdefinition is left normally (via the end),a
9.3.2. Class Objects
类对象,支持两种操作,attribute references 和instantiation
Class objectssupport two kinds of operations: attribute references andinstantiation.
Attributereferences
attributereferences 是标准的语法,可以这样引用:obj.name
then
Class
第二种方法instantiation要用function来标记才行。调用函数以后会返回一个实例。可以像下面一样调用了
creates anew
The instantiationoperation (“calling” a class object) creates an empty object. Manyclasses like to create objects with instances customized to aspecific initial state. Therefore a class may define a specialmethod named__init__(),like this:
When a classdefines 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 derivedclass definition looks like this:
一个没有继承的class就不能称为class
The name
基类的名字就在上面的例子中定义,代替掉基类的名称,其他表达式也是被允许的。下面看看例子
Execution of a derived class definition proceeds the same as for abase class. When the class object is constructed, the base class isremembered. This is used for resolving attribute references: if arequested attribute is not found in the class, the search proceedsto look in the base class. This rule is applied recursively if thebase class itself is derived from some other class.
There’s nothing special about instantiation of derivedclasses:
Derived classes may override methods of their base classes. Becausemethods have no special privileges when calling other methods ofthe same object, a method of a base class that calls another methoddefined in the same base class may end up calling a method of aderived class that overrides it. (For C++ programmers: all methodsin Python are effectively
An overriding method in a derived class may in fact want to extendrather than simply replace the base class method of the same name.There is a simple way to call the base class method directly: justcall
Python has two built-in functions that work with inheritance:
- Use
isinstance() tocheck an instance’stype: isinstance(obj, int) willbe True onlyif obj.__class__ is int orsome class derivedfrom int. - Use
issubclass() tocheck classinheritance: issubclass(bool, int) is True since bool isa subclass of int.However,issubclass(unicode, str) is False since unicode isnot a subclass of str (theyonly share a common ancestor,basestring).
9.6. Private Variables
private这个实例变量不能随便被修改,除非不纯在一个对象里边。通常用带下划线前缀名称标识非公共API的部分。
“Private”instance variables that cannot be accessed except from inside anobject don’t exist in Python. However, there is a convention thatis followed by most Python code: a name prefixed with an underscore(e.g.
Since there is avalid use-case for class-private members (namely to avoid nameclashes of names with names defined by subclasses), there islimited support for such a mechanism, called
Name mangling ishelpful for letting subclasses override methods without breakingintraclass method calls. For example:
Note that themangling rules are designed mostly to avoid accidents; it still ispossible to access or modify a variable that is considered private.This can even be useful in special circumstances, such as in thedebugger.
Notice that codepassed to
- The Python Tutorial 9. Classes
- The Python Tutorial 4. More Con…
- The Python Tutorial 6. Modules …
- The Python Tutorial » 5. D…
- A Beginner's Python Tutorial
- HDOJ 1709 The Balance
-  
- HDOJ 1597 find the nth digit
- Facebook Share iOS Tutorial
-
- 【原创】python The pac…
- POJ 3185 The Water Bowls
- POJ 1050 To the Max
- HDU 1405 The Last Practice
- POJ 2945 Find the Clones
- dp 3267 The Cow Lexicon
- HDOJ 1010 Tempter of the Bone
- Subversion - A Quick Tutorial
- The Python Tutorial 4. More Con…
- The Python Tutorial » 5. D…
- 中国城市最新排名:一线、二线、三…
- The Python Tutorial 6. Modules …
- Annotation介绍@interface
- The Python Tutorial 9. Classes
- Coding Python GNU Radio Applicat…
- Python 里边的parser用法
- IsWindow函数的作用
- wamp配置多域名
- 双系统 启动顺序(xp / linux)
- 485通信的电路小问题
- tl431 输出接104 振荡
- 三极管搭的逻辑门