您的位置:首页 > 编程语言 > Python开发

别人的python类的笔记

2016-06-16 00:00 441 查看
摘要: python 类

笔记1:
在类主体执行期间创建的值放在类对象中,对象充当命名空间
在类中,需要显示的使用 self

笔记2:
在搜索一个属性时,如果未在实例或者实例的类中找到匹配的项,会向基类(父类)搜索
有深度优先和广度优先两种搜索原则

笔记3:
派生类定义__init__()时候,不会自动调用基类的__init__()的方法,调用方法Account.__init__(self, name, balance)
要指定调用基类的方法,需使用 super 调用
eg:super(MoreEvilAccount, self).deposit(1000)

笔记4:
当存在多继承时候,如果多个基类都实现了同名的方法,则通过 super 无法指定调用哪个基类方法,可以通过(类名.方法)调用
通过 super 指定调用是按照继承的搜索顺序调用的
使用__mro__内置属性可以得出基类搜索顺序

笔记5:
类定义静态方法,使用@staticmethod
类定义类方法,使用@classmethod
将方法定义成对象特性,@property

对于一般的私有变量可以使用 property 来开放访问接口
@property
@name .setter
@name.deleter

eg:::
@property
def name(self):
return self.__name

@name.setter
def name(self, vl):
self.__name = vl

@name.deleter
def name(self):
print 'delete name'
eg:::end

笔记6:
类的封装和私有属性,
Python 类默认都是公开的访问属性,以'__'开头不以'__'(如__ Foo)结尾属性和方法为私有属性,会形成具有'_类名__Foo'形式的新名称,
在模块中使用以'_'开头定义属性和方法,使得对模块来说是私有的,不能通过 from xx import * 引入

笔记7:
类创建可以使用__new__()方法创建, eg::f = Foo.__new__(Foo, 11)

笔记8:
在类的内部使用字典来实现,可以通过__dict__内部属性来访问该字典,在修改实例属性的时候会反映到__ dict__ 属性,同时如果修改__dict__也会反映到实例属性的修改上

笔记9:
在类中使用特殊变量__slots__可以限制实例属性名称的设置,使用__slots__会使效率更快
eg:::
class TAccount(object):
__slots__ = ('name', 'balance')

def __init__(self, name, balance):
self.th = name #此操作会报错

taccount = TAccount('cst', 78)
taccount.gg = 90 #此操作也报错
eg:::end

当类继承了使用__slots__的基类时,该类自己也需要使用__slots__类存储自己的属性(既是不添加属性),否则运行效率将会很慢

笔记10:
对于类重新定义一些内置的方法可以使运算符重载,如__add__,__sub__
重载__instancecheck__()或__subclasscheck__()可以重载类的测试方法

笔记11:
抽象基类
定义抽象基类需要使用 abc模块,同时需要定义类元类(__metaclass__)变量__metaclass__,eg:::__metaclass__ = ABCMeta
抽象基类无法实例化,否则报'TypeError: Can't instantiate abstract class'错误
eg:::
from abc import ABCMeta, abstractmethod, abstractproperty
class ABCFoo():
__metaclass__ = ABCMeta

@staticmethod
def stamethod():
print 'static method'

@abstractmethod
def spam(self, a, b):
pass

@abstractproperty
def name(self):
pass

class IABCFoo(ABCFoo):

def spam(self, a, b):
ABCFoo.stamethod()
print 'spam iabc foo'

@property
def name(self):
return 'name'
eg:::end

子类的元类变量和基类变量一样的,没有基类将寻找全局(__metaclass__)变量

'''

class Account(object):
num_accounts = 0

def __init__(self, name, balance):
self.name = name
self.balance = balance
Account.num_accounts += 1

def deposit(self, amt):
self.balance += amt

def withdraw(self, amt):
self.balance -= amt

def inquiry(self):
return self.balance

class EvilAccount(Account):
def __init__(self, name, balance, evilfactor):
Account.__init__(self, name, balance)
self.evilfactor = evilfactor

def inquiry(self):
return self.balance * 1.2

class JieDaiAccount():

def deposit(self, amt):
self.balance += amt * 3

class MoreEvilAccount(EvilAccount, JieDaiAccount, ):

def deposit(self, amt):
self.withdraw(5.0)
JieDaiAccount.deposit(self, 1000)
#super(JieDaiAccount, self).deposit(1000)

class Foo(object):

def __init__(self,name):
self.__name = name

@classmethod
def clsinfo(cls, msg = None):
print 'class method msg : %s' % msg

@staticmethod
def stainfo():
print 'static method .......'

@property
def name(self):
return self.__name

@name.setter
def name(self, vl):
self.__name = vl

@name.deleter
def name(self):
print 'delete name'

class TAccount(object):
__slots__ = ('th', 'name', 'balance')

def __init__(self, name, balance):
self.th = name

from abc import ABCMeta, abstractmethod, abstractproperty
class ABCFoo():
__metaclass__ = ABCMeta

@staticmethod
def stamethod():
print 'static method'

@abstractmethod
def spam(self, a, b):
pass

@abstractproperty
def name(self):
pass

class IABCFoo(ABCFoo):

def spam(self, a, b):
ABCFoo.stamethod()
print 'spam iabc foo'

@property
def name(self):
return 'name'

if __name__ == '__main__':
a = Account('cst', 10000)
b = Account('uuuu', 2000)
print a.inquiry()
print b.inquiry()

c = EvilAccount('cst2', 3000, 90)
print c.evilfactor
print c.inquiry()

me = MoreEvilAccount('hhhh', 8000, 90)
me.deposit(1000)
print me.balance

print MoreEvilAccount.__mro__

f = Foo('cssss')

Foo.clsinfo('temp')
Foo.stainfo()
f.name = '787'
print f.name
del f.name

taccount = TAccount('cst', 78)

iabc = IABCFoo()
print iabc.name
iabc.spam(7, 88)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python