- 浏览: 348246 次
- 性别:
- 来自: 福州
文章分类
最新评论
-
18215361994:
chrome扩展中可以获取开发者工具所有资源不?
Chrome 插件开发 -
lBovinl:
完全是一头雾水~o(︶︿︶)o 唉
乱七八糟的Ruby笔记 -
zhangyanan_it:
楼主总结的好多!想问下:map.resources :role ...
Rails随记 -
justin_chiang:
...
Propertiess资源文件的读取和简单的性能测试 -
lezi2012:
感谢分享!!!感谢分享!!!感谢分享!!!感谢分享!!!感谢 ...
Rails随记
Chapter 22
OOP: The Big Picture
Python中class的3个关键特色
1:多实例
2:用户自定义继承(多继承)
3:运算符重载
Python中的继承关系,会按照从上到下,从左到右的顺序搜索属性和方法
和module不同,class可以在内容拥有多个实例
Python会将class实例的方法调用,转换成class.方法(实例)的调用
_ _init_ _方法类似Java中的构造函数,可以用于初始化对象
本章只是大概介绍了Python Class相关的一些内容,比较大概..
凑字数问题
Chapter Quiz
1. What is the main point of OOP in Python?
2. Where does an inheritance search look for an attribute?
3. What is the difference between a class object and an instance object?
4. Why is the first argument in a class method function special?
5. What is the _ _init_ _ method used for?
6. How do you create a class instance?
7. How do you create a class?
8. How do you specify a class’ superclasses?
Quiz Answers
1. OOP is about code reuse—you factor code to minimize redundancy and program
by customizing what already exists instead of changing code in-place or
starting from scratch.
2. An inheritance search looks for an attribute first in the instance object, then in the class the instance was created from, then in all higher superclasses, progressing from the top to the bottom of the object tree, and from left to right (by default). The search stops at the first place the attribute is found. Because the lowest version of a name found along the way wins, class hierarchies naturally
support customization by extension.
3. Both class and instance objects are namespaces (packages of variables that
appear as attributes). The main difference between them is that classes are a kind
of factory for creating multiple instances. Classes also support operator overloading methods, which instances inherit, and treat any functions nested within them as special methods for processing instances.
4. The first argument in a class method function is special because it always
receives the instance object that is the implied subject of the method call. It’s
usually called self by convention. Because method functions always have this
implied subject object context by default, we say they are “object oriented”—i.e.,designed to process or change objects.
5. If the _ _init_ _ method is coded or inherited in a class, Python calls it automatically each time an instance of that class is created. It’s known as the constructor method; it is passed the new instance implicitly, as well as any arguments passed explicitly to the class name. It’s also the most commonly used operator overloading method. If no _ _init_ _ method is present, instances simply begin life as empty namespaces.
6. You create a class instance by calling the class name as though it were a function; any arguments passed into the class name show up as arguments two and
beyond in the _ _init_ _ constructor method. The new instance remembers the
class it was created from for inheritance purposes.
7. You create a class by running a class statement; like function definitions, these statements normally run when the enclosing module file is imported (more on
this in the next chapter).
8. You specify a class’ superclasses by listing them in parentheses in the class statement, after the new class’ name. The left-to-right order in which the classes are listed in the parentheses gives the left-to-right inheritance search order in the class tree.
Chapter 23
Class Coding Basics
类编码基础
两种Python OOP基础的对象
Class object , instance objects
通过class修改类的属性之后,将会影响所有后续生成对象的状态
在定义类方法时,注意使用self.属性进行实例域的修改
可以任意给class实例添加对象.通过self
类属于模块的一部分,所以使用时,需要import
类对象支持运算符重载,通过重写特殊的__x__方法,运算符重载不是必须的,不过如有没有重载,调用对应的
运算符将会抛出异常
不过不建议因为觉得 cool,而滥用运算符重载,增加代码复杂度
重载的例子
def _ _add_ _(self, other): # On "self + other"
return ThirdClass(self.data + other)
def _ _mul_ _(self, other):
self.data = self.data * other # On "self * other"
创建一个最简单的Class
class rec: pass // ()不是必须的
可以通过类名,随意添加属性
rec.name = 'Bob'
Class对象的一些属性
x.__class__ x类所继承的类对象
_ _bases_ _ 使用tuple表示类的父类对象
凑字数问题省略,本章只是介绍了一些class的特性...
Chapter 24
Class Coding Details
深入Class编码
类的常见格式
class <name>(superclass,...): # Assign to name
data = value # Shared class data
def method(self,...): # Methods
self.member = value # Per-instance data
_ _init_ _ function 类似Java中的构造函数
对类.属性进行修改,会影响到所有实例的值,而对实例进行修改将不会修改到其他实例和类的状态
方法调用的自动转换
instance.method(args...)
class.method(instance, args...)
调用父类的构造函数
Super.__init__(self, x)
常见的运算符重载方法
Method Overloads Called for
_ _init_ _ Constructor Object creation: X= Class( )
_ _del_ _ Destructor Object reclamation
_ _add_ _ Operator + X+Y, X+= Y
_ _or_ _ Operator | (bitwise OR) X | Y, X|= Y
_ _repr_ _,_ _str_ _ Printing, conversions print X, repr(X), str(X)
_ _call_ _ Function calls X ( )
_ _getattr_ _ Qualification X.undefined
_ _setattr_ _ Attribute assignment X.any = value
_ _getitem_ _ Indexing X[key], for loops and other iterations if no _ _iter_ _
_ _setitem_ _ Index assignment X[key] = value
_ _len_ _ Length len(X), truth tests
_ _cmp_ _ Comparison X == Y, X < Y
_ _lt_ _ Specific comparison X < Y (or else _ _cmp_ _)
_ _eq_ _ Specific comparison X== Y (or else _ _cmp_ _)
_ _radd_ _ Right-side operator + Noninstance + X
_ _iadd_ _ In-place (augmented) addition X+= Y (or else _ _add_ _)
_ _iter_ _ Iteration contexts for loops, in tests, list comprehensions, map, others
常用方法
__init__ 构造函数
_ _getitem_ _(self,index) 用于提供for..循环支持,需要先提供_ _iter_ _方法
def _ _iter_ _(self): # Get iterator object on iter( )
return self
也可以通过yield实现
_ _getattr_ _ and _ _setattr_ _ 用于对属性的设置进行过滤,会拦截所有的属性设置和获取
def _ _setattr_ _(self, attr, value):
if attr == 'age':
self._ _dict_ _[attr] = value //注意使用这种方式赋值
else:
raise AttributeError, attr + ' not allowed'
可以通过上述方法,放置用户修改私有属性..模拟
_ _repr_ _ and _ _str_ _ 用于返回对应内置函数调用时的字符串返回结果
def _ _radd_ _(self, other):
... print 'radd', self.val, other
用于在 1+object时其作用,对象位于+的右边
def _ _call_ _(self, other):
... return self.value * other
用于在class的实例()时被调用
_ _del_ _ 用于垃圾回收
事件机制,当没有引用直接向该对象,该方法会自动被调用,并不常用..
def _ _del_ _(self):
凑字数问题省略,本章涉及的问题慢慢变的抽象了,也许可以称为高级特性
Chapter 25
Designing with Classes
类设计
可以使用空属性和方法的类,用于存储数据,包括了类与实例
class rec: pass
方法也是对象,分为Bound 和 Unbound两种
区分的方式为: 看方法定义的参数中是否包括了self
docstring 可以放置在module,class,method里面使用
module.spam._ _doc_ _// 任意节点进行访问
使用比#方便,支持多行文本
章节问题:
Chapter Quiz
1. What is multiple inheritance?
2. What is delegation?
3. What is composition?
4. What are bound methods?
Quiz Answers
1. Multiple inheritance occurs when a class inherits from more than one superclass;it’s useful for mixing together multiple packages of class-based code.
2. Delegation involves wrapping an object in a proxy class, which adds extra
behavior, and passes other operations to the wrapped object. The proxy retains
the interface of the wrapped object.
3. Composition is a technique whereby a controller class embeds and directs a
number of objects, and provides an interface all its own; it’s a way to build up
larger structures with classes.
4. Bound methods combine an instance and a method function; you can call them
without passing in an instance object explicitly because the original instance is
still available.
Chapter 26
Advanced Class Topics
使用__a 用于创建私有变量,不能直接使用__a进行访问,需要使用self.__a或使用self.classname__a 进行访问
在多继承的时候,可以使用__x变量代替x,独立的作用域,防止多个类之间的变量名冲突
在多人开发的项目中,尤其注意使用上述方法,避免不必要的冲突导致的bug
注意在使用class继承的时候,为了防止父类指向参数名称冲突,可以使用明确的变量名在子类中进行指定..
>>> class A(object): attr = 1 # New style
>>> class B(A): pass
>>> class C(A): attr = 2
>>> class D(B,C): attr = B.attr # Choose A.attr, above
>>> x = D( )
>>> x.attr # Works like classic
1
同样,对于method也可以采用这种方法
使用__slots__限制动态属性的添加
class x(object)
_ _slots_ _ = ['age', 'name', 'job']
用于指定所允许设置的动态属性
x.age = 40 如果属性不存在slots中,将会抛出异常
__setattr__和__getattr__方法也可以达到同样的效果
使用new style的方式定义类,也就是在传统的class定义时,明确定义父类为object
class newprops(object):
在new style中可以使用类似Java的get和set方式去设置属性,更为简单一些,注意父类(object)是必须的
class newprops(object): //必须有object
def getage(self):
return 40
def setage(self, value):
print 'set age:', value
self._age = value
age = property(getage, setage, None, None) //后面两个None不是必须的
在实例化后,使用x.age = 42 可以进行赋值时,将会自动调用对于的set方法
同样 也可以通过重写__setattr__和__getattr__方法实现,注意set方法的设置方式比较特殊
self._ _dict_ _['_age'] = value,字典的方式进行赋值
使用__getattr..和__setattr 会更灵活一些
创建staticmethod与classmethod:
def smeth(x): # Static: no instance passed
print x
def cmeth(cls, x): # Class: gets class, not instance
print cls, x
smeth = staticmethod(smeth) # Make smeth a static method
cmeth = classmethod(cmeth) # Make cmeth a class method.
如果不加上staticmethod() 与classmethod() 进行方法的转换,那么直接调用时,会强制要求self的第一个参数
也可以使用注解进行定义staticmethod
@staticmethod
def smeth(x):
自定义Decorator 需要使用class来定义,而不是方法,而且构造函数需要function为参数传入
必须实现的有
1:带function的构造__init__函数
2:__call__方法
class der():
def __init__(self,func):
self.call=0
self.func=func
def __call__(self,*args):
self.call+=1
print 'def call'
self.func(*args)
添加注解后,该function就变成能够静态调用
使用多继承的情况下,如果存在方法同名,建议在子类中明确指定子类中使用哪个父类方法
保持类的层次不要太深,少用继承,便于阅读与维护
凑字数问题省略
OOP: The Big Picture
Python中class的3个关键特色
1:多实例
2:用户自定义继承(多继承)
3:运算符重载
Python中的继承关系,会按照从上到下,从左到右的顺序搜索属性和方法
和module不同,class可以在内容拥有多个实例
Python会将class实例的方法调用,转换成class.方法(实例)的调用
_ _init_ _方法类似Java中的构造函数,可以用于初始化对象
本章只是大概介绍了Python Class相关的一些内容,比较大概..
凑字数问题
Chapter Quiz
1. What is the main point of OOP in Python?
2. Where does an inheritance search look for an attribute?
3. What is the difference between a class object and an instance object?
4. Why is the first argument in a class method function special?
5. What is the _ _init_ _ method used for?
6. How do you create a class instance?
7. How do you create a class?
8. How do you specify a class’ superclasses?
Quiz Answers
1. OOP is about code reuse—you factor code to minimize redundancy and program
by customizing what already exists instead of changing code in-place or
starting from scratch.
2. An inheritance search looks for an attribute first in the instance object, then in the class the instance was created from, then in all higher superclasses, progressing from the top to the bottom of the object tree, and from left to right (by default). The search stops at the first place the attribute is found. Because the lowest version of a name found along the way wins, class hierarchies naturally
support customization by extension.
3. Both class and instance objects are namespaces (packages of variables that
appear as attributes). The main difference between them is that classes are a kind
of factory for creating multiple instances. Classes also support operator overloading methods, which instances inherit, and treat any functions nested within them as special methods for processing instances.
4. The first argument in a class method function is special because it always
receives the instance object that is the implied subject of the method call. It’s
usually called self by convention. Because method functions always have this
implied subject object context by default, we say they are “object oriented”—i.e.,designed to process or change objects.
5. If the _ _init_ _ method is coded or inherited in a class, Python calls it automatically each time an instance of that class is created. It’s known as the constructor method; it is passed the new instance implicitly, as well as any arguments passed explicitly to the class name. It’s also the most commonly used operator overloading method. If no _ _init_ _ method is present, instances simply begin life as empty namespaces.
6. You create a class instance by calling the class name as though it were a function; any arguments passed into the class name show up as arguments two and
beyond in the _ _init_ _ constructor method. The new instance remembers the
class it was created from for inheritance purposes.
7. You create a class by running a class statement; like function definitions, these statements normally run when the enclosing module file is imported (more on
this in the next chapter).
8. You specify a class’ superclasses by listing them in parentheses in the class statement, after the new class’ name. The left-to-right order in which the classes are listed in the parentheses gives the left-to-right inheritance search order in the class tree.
Chapter 23
Class Coding Basics
类编码基础
两种Python OOP基础的对象
Class object , instance objects
通过class修改类的属性之后,将会影响所有后续生成对象的状态
在定义类方法时,注意使用self.属性进行实例域的修改
可以任意给class实例添加对象.通过self
类属于模块的一部分,所以使用时,需要import
类对象支持运算符重载,通过重写特殊的__x__方法,运算符重载不是必须的,不过如有没有重载,调用对应的
运算符将会抛出异常
不过不建议因为觉得 cool,而滥用运算符重载,增加代码复杂度
重载的例子
def _ _add_ _(self, other): # On "self + other"
return ThirdClass(self.data + other)
def _ _mul_ _(self, other):
self.data = self.data * other # On "self * other"
创建一个最简单的Class
class rec: pass // ()不是必须的
可以通过类名,随意添加属性
rec.name = 'Bob'
Class对象的一些属性
x.__class__ x类所继承的类对象
_ _bases_ _ 使用tuple表示类的父类对象
凑字数问题省略,本章只是介绍了一些class的特性...
Chapter 24
Class Coding Details
深入Class编码
类的常见格式
class <name>(superclass,...): # Assign to name
data = value # Shared class data
def method(self,...): # Methods
self.member = value # Per-instance data
_ _init_ _ function 类似Java中的构造函数
对类.属性进行修改,会影响到所有实例的值,而对实例进行修改将不会修改到其他实例和类的状态
方法调用的自动转换
instance.method(args...)
class.method(instance, args...)
调用父类的构造函数
Super.__init__(self, x)
常见的运算符重载方法
Method Overloads Called for
_ _init_ _ Constructor Object creation: X= Class( )
_ _del_ _ Destructor Object reclamation
_ _add_ _ Operator + X+Y, X+= Y
_ _or_ _ Operator | (bitwise OR) X | Y, X|= Y
_ _repr_ _,_ _str_ _ Printing, conversions print X, repr(X), str(X)
_ _call_ _ Function calls X ( )
_ _getattr_ _ Qualification X.undefined
_ _setattr_ _ Attribute assignment X.any = value
_ _getitem_ _ Indexing X[key], for loops and other iterations if no _ _iter_ _
_ _setitem_ _ Index assignment X[key] = value
_ _len_ _ Length len(X), truth tests
_ _cmp_ _ Comparison X == Y, X < Y
_ _lt_ _ Specific comparison X < Y (or else _ _cmp_ _)
_ _eq_ _ Specific comparison X== Y (or else _ _cmp_ _)
_ _radd_ _ Right-side operator + Noninstance + X
_ _iadd_ _ In-place (augmented) addition X+= Y (or else _ _add_ _)
_ _iter_ _ Iteration contexts for loops, in tests, list comprehensions, map, others
常用方法
__init__ 构造函数
_ _getitem_ _(self,index) 用于提供for..循环支持,需要先提供_ _iter_ _方法
def _ _iter_ _(self): # Get iterator object on iter( )
return self
也可以通过yield实现
_ _getattr_ _ and _ _setattr_ _ 用于对属性的设置进行过滤,会拦截所有的属性设置和获取
def _ _setattr_ _(self, attr, value):
if attr == 'age':
self._ _dict_ _[attr] = value //注意使用这种方式赋值
else:
raise AttributeError, attr + ' not allowed'
可以通过上述方法,放置用户修改私有属性..模拟
_ _repr_ _ and _ _str_ _ 用于返回对应内置函数调用时的字符串返回结果
def _ _radd_ _(self, other):
... print 'radd', self.val, other
用于在 1+object时其作用,对象位于+的右边
def _ _call_ _(self, other):
... return self.value * other
用于在class的实例()时被调用
_ _del_ _ 用于垃圾回收
事件机制,当没有引用直接向该对象,该方法会自动被调用,并不常用..
def _ _del_ _(self):
凑字数问题省略,本章涉及的问题慢慢变的抽象了,也许可以称为高级特性
Chapter 25
Designing with Classes
类设计
可以使用空属性和方法的类,用于存储数据,包括了类与实例
class rec: pass
方法也是对象,分为Bound 和 Unbound两种
区分的方式为: 看方法定义的参数中是否包括了self
docstring 可以放置在module,class,method里面使用
module.spam._ _doc_ _// 任意节点进行访问
使用比#方便,支持多行文本
章节问题:
Chapter Quiz
1. What is multiple inheritance?
2. What is delegation?
3. What is composition?
4. What are bound methods?
Quiz Answers
1. Multiple inheritance occurs when a class inherits from more than one superclass;it’s useful for mixing together multiple packages of class-based code.
2. Delegation involves wrapping an object in a proxy class, which adds extra
behavior, and passes other operations to the wrapped object. The proxy retains
the interface of the wrapped object.
3. Composition is a technique whereby a controller class embeds and directs a
number of objects, and provides an interface all its own; it’s a way to build up
larger structures with classes.
4. Bound methods combine an instance and a method function; you can call them
without passing in an instance object explicitly because the original instance is
still available.
Chapter 26
Advanced Class Topics
使用__a 用于创建私有变量,不能直接使用__a进行访问,需要使用self.__a或使用self.classname__a 进行访问
在多继承的时候,可以使用__x变量代替x,独立的作用域,防止多个类之间的变量名冲突
在多人开发的项目中,尤其注意使用上述方法,避免不必要的冲突导致的bug
注意在使用class继承的时候,为了防止父类指向参数名称冲突,可以使用明确的变量名在子类中进行指定..
>>> class A(object): attr = 1 # New style
>>> class B(A): pass
>>> class C(A): attr = 2
>>> class D(B,C): attr = B.attr # Choose A.attr, above
>>> x = D( )
>>> x.attr # Works like classic
1
同样,对于method也可以采用这种方法
使用__slots__限制动态属性的添加
class x(object)
_ _slots_ _ = ['age', 'name', 'job']
用于指定所允许设置的动态属性
x.age = 40 如果属性不存在slots中,将会抛出异常
__setattr__和__getattr__方法也可以达到同样的效果
使用new style的方式定义类,也就是在传统的class定义时,明确定义父类为object
class newprops(object):
在new style中可以使用类似Java的get和set方式去设置属性,更为简单一些,注意父类(object)是必须的
class newprops(object): //必须有object
def getage(self):
return 40
def setage(self, value):
print 'set age:', value
self._age = value
age = property(getage, setage, None, None) //后面两个None不是必须的
在实例化后,使用x.age = 42 可以进行赋值时,将会自动调用对于的set方法
同样 也可以通过重写__setattr__和__getattr__方法实现,注意set方法的设置方式比较特殊
self._ _dict_ _['_age'] = value,字典的方式进行赋值
使用__getattr..和__setattr 会更灵活一些
创建staticmethod与classmethod:
def smeth(x): # Static: no instance passed
print x
def cmeth(cls, x): # Class: gets class, not instance
print cls, x
smeth = staticmethod(smeth) # Make smeth a static method
cmeth = classmethod(cmeth) # Make cmeth a class method.
如果不加上staticmethod() 与classmethod() 进行方法的转换,那么直接调用时,会强制要求self的第一个参数
也可以使用注解进行定义staticmethod
@staticmethod
def smeth(x):
自定义Decorator 需要使用class来定义,而不是方法,而且构造函数需要function为参数传入
必须实现的有
1:带function的构造__init__函数
2:__call__方法
class der():
def __init__(self,func):
self.call=0
self.func=func
def __call__(self,*args):
self.call+=1
print 'def call'
self.func(*args)
添加注解后,该function就变成能够静态调用
使用多继承的情况下,如果存在方法同名,建议在子类中明确指定子类中使用哪个父类方法
保持类的层次不要太深,少用继承,便于阅读与维护
凑字数问题省略
发表评论
-
dive into python中文版笔记
2009-08-10 15:39 2646本文为dive into python中文版笔记--感谢翻译人 ... -
Learning.Python.3rd.Edition 笔记[27-29]
2009-08-04 17:16 1050Chapter 27 Exception Basics 本书 ... -
Learning.Python.3rd.Edition 笔记 21章节
2009-07-30 10:55 1230Chapter 18 Modules: The Big Pic ... -
Learning.Python.3rd.Edition 笔记[14:17]
2009-07-28 18:41 1213Chapter 15 Function Basics 函数基 ... -
Learning.Python.3rd.Edition 笔记[9:14]
2009-07-24 17:17 1264第三部分 Statements and Syn ... -
Learning.Python.3rd.Edition 笔记[7:8]
2009-07-22 19:29 1471Chapter 8 Lists and Dictionarie ... -
Learning.Python.3rd.Edition 笔记[5:7]
2009-07-22 11:42 1490The Dynamic Typing Interlude a ... -
Learning.Python.3rd.Edition 笔记[4]
2009-07-21 11:28 1295第四章中很大概的介绍了下Python的core 数据类型,后续 ... -
Learning.Python.3rd.Edition 笔记 [0:4]
2009-07-20 17:05 1516前三章介绍Python的历史, ... -
简明 Python 笔记
2009-07-16 19:56 1874版本较旧 2.34,可能有已被修改的 数值类型: 在Pyt ... -
Python Totorial入门指南 笔记 [9:]
2009-07-16 13:42 1032实现自定义class的迭代 ... -
Python Totorial入门指南 笔记 [5:9]
2009-07-15 19:44 1046使用关键字import导入模 ... -
Python Totorial入门指南 笔记 :5
2009-07-14 19:55 1033在Window xp上,需要在环境 ...
相关推荐
Packt.Python.Machine.Learning.3rd.2019.12.mobi
Key Features Written to the latest, gold-standard ...Edition: 3rd Revised edition Language: English Publisher: Packt Publishing Publication Date: 2017-03-06 ISBN-10: 1786469715 ISBN-13: 9781786469717
《Python学习手册(第4版)》由Mark Lutz撰写,是Python编程语言学习的经典教材之一,本书深入探讨了Python的基础知识与高级概念,适用于初学者到有经验的程序员。以下是从该书标题、描述、标签及部分内容中提取的...
Packt.Python.Machine.Learning.Cookbook.2nd.Edition.2019
### 关于《Packt.Python.Machine.Learning.2015》的知识点解析 #### 一、书籍基本信息概述 本书名为《Packt.Python.Machine.Learning.2015》,由Sebastian Raschka编写,版权归属于Packt Publishing,并在2015年...
Title: Building Machine Learning Systems with Python, 2nd Edition Author: Willi Richert Length: 326 pages Edition: 1 Language: English Publisher: Packt Publishing Publication Date: 2015-03-31 ISBN-10:...
Manning.Deep.Learning.with.Python.2nd.Edition(机翻)
learning.python.2nd.edition
Python.Machine.Learning.2nd.Edition.2017.9 包含3种格式: 1. Python.Machine.Learning.2nd.Edition.2017.9.pdf 2. Python.Machine.Learning.2nd.Edition.2017.9.epub 3. Python.Machine.Learning.2nd.Edition....
《Learning.JavaScript.3rd.Edition》是一本深入学习JavaScript编程语言的重要著作,适用于初学者和有经验的开发者。本书的第三版详细介绍了JavaScript的核心概念和技术,为读者提供了全面而深入的理解,帮助他们...
Manning.Deep.Learning.with.Python.2017.11 原版 Deep Learning with Python introduces the field of deep learning using the Python language and the powerful Keras library. Written by Keras creator and ...