马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
1.issubclass(class, classinfo)
1)一个类被认为是其自身的子类;
2)classinfo可以是类对象组成的元组,只要class与其中任何一个候选类的子类,则返回True;>>> class A:
pass
>>> class B(A):
pass
>>> issubclass(B, A)
True
>>> issubclass(B, B)
True
>>> issubclass(B, object)
True
>>> class C:
pass
>>> issubclass(B, C)
False
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 "<pyshell#27>", line 1, in <module>
hasattr(c1, x)
NameError: name 'x' is not defined
4.getattr(object, name[, default])
返回一个对象内某个成员的值>>> getattr(c1, 'x')
0
>>> getattr(c1, 'y')
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
getattr(c1, 'y')
AttributeError: 'C' object has no attribute 'y'
>>> getattr(c1, 'y', '您所访问的属性不存在...')
'您所访问的属性不存在...'
5.setattr(object, name, value)
设置一个对象内某个变量的值>>> setattr(c1, 'y', 'FishC')
>>> getattr(c1, 'y', '您所访问的属性不存在...')
'FishC'
6.delattr(object, name)
删除一个对象的某个变量>>> delattr(c1, 'y')
>>> delattr(c1, 'y')
Traceback (most recent call last):
File "<pyshell#34>", line 1, in <module>
delattr(c1, 'y')
AttributeError: y
7.property(fget=None, fest=None, fde1=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
>>> del c1.x
>>> c1.size
Traceback (most recent call last):
File "<pyshell#53>", line 1, in <module>
c1.size
AttributeError: 'C' object has no attribute 'size'
|