标签:erro def back pytho class object default property 参数
1. issubclass(class,classinfo)
一个类被认为是其自身的子类
classinfo可以是类对象组成的元组,只要class与其中任何一个候选类的子类,则返回True
其他情况TypeError
>>> class A(): ... pass ... >>> class B(A): ... pass ... >>> issubclass(B,A) True >>> issubclass(B,B) True >>> issubclass(A,B) False >>> issubclass(A,object) True
2. isinstance(object, classinfo)
1.如果第一参数不是对象,则永远返回False
2.如果第二个参数不是类或者由类对象组成的元组,会抛出一个TypeError异常
>>> b1 = B() >>> isinstance(b1,B) True >>> isinstance(b1,A) True >>> isinstance(b1,C) False >>> isinstance(b1,(A,B,C)) True
3.hasattr(object,name)
attr = attribute : 属性
测试对象是否有相应的属性
>>> class C: ... def __init__(self,x=0): ... self.x = x ... >>> c1 = C() >>> hasattr(c1,‘x‘) True >>> hasattr(c1,x) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name ‘x‘ is not defined
4.getattr(object,name[,default])
访问对象的属性值,如果不存在返回设置的默认值,否则会抛出AttrubuteError
>>> getattr(c1,‘x‘) 0 >>> getattr(c1,‘y‘,‘您所访问的属性不存在...‘) ‘您所访问的属性不存在...‘
5.setattr(object,name,value)
设置对象的属性值,如果不存在,将创建属性并赋值
>>> setattr(c1, ‘x‘, ‘5‘) >>> getattr(c1, ‘x‘) ‘5‘ >>> setattr(c1, ‘y‘, ‘10‘) >>> getattr(c1, ‘y‘) ‘10‘
6.delattr(object, name)
>>> delattr(c1,‘y‘) >>> getattr(c1,‘y‘,‘访问属性不存在‘) ‘访问属性不存在‘
7.property(fget = None, fset = None, fdel = None, doc = None)
作用:通过属性设置属性
>>> class C: ... def __init__(self,size = 10): ... self.size = size ... def getSize(self): ... return self.size ... def setSize(self,value): ... self.size = value ... def delSize(self): ... del self.size ... x = property(getSize, setSize, delSize) ... >>> c1 = C() >>> c1.getSize() 10 >>> c1.x 10 >>> c1.x = 18 >>> c1.x 18 >>> c1.size 18 >>> c1.getSize() 18
>>> del c1.x
>>> c1.size
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: ‘C‘ object has no attribute ‘size‘
标签:erro def back pytho class object default property 参数
原文地址:http://www.cnblogs.com/fengjunjie-w/p/7509874.html