Learning.Python.3rd.Edition 笔记[22-26]章节

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就变成能够静态调用

使用多继承的情况下,如果存在方法同名,建议在子类中明确指定子类中使用哪个父类方法

保持类的层次不要太深,少用继承,便于阅读与维护

凑字数问题省略
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值