熬死你的 发表于 2015-12-1 15:04:52

python中self.__class__

  1. python中的self
  python中的self就相当于C++中的this指针
也就是指向对象本身的指针
self.name = name 就是当前对象的成员变量name赋值为name。
  
  2.python的self.__class__
  表示当前实例对象的类.
  例如:



if hasattr(self.__class__, 'fields') and len(self.__class__.fields) > 0:
  
  3. hasattr():



hasattr用于确定一个对象是否具有某个属性。
语法:
hasattr(object, name) -> bool
判断object中是否有name属性,返回一个布尔值。
>>> li=["zhangjing","zhangwei"]
>>> getattr(li,"pop")
<built-in method pop of list object at 0x011DF6C0>
>>> li.pop
<built-in method pop of list object at 0x011DF6C0>
>>> li.pop()
'zhangwei'
>>> getattr(li,"pop")()
'zhangjing'
>>>getattr(li, "append")("Moe")
  
页: [1]
查看完整版本: python中self.__class__