首页 > 编程语言 > 详细

python中self.__class__

时间:2015-06-29 19:18:28      阅读:419      评论:0      收藏:0      [点我收藏+]

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") 

 

python中self.__class__

原文:http://www.cnblogs.com/blogofwyl/p/4607792.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!