免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 808 | 回复: 0
打印 上一主题 下一主题

Python类中的特殊方法 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-03-24 09:56 |只看该作者 |倒序浏览

                __init__(self,...)      这个方法在新建对象恰好要被返回使用之前被调用。
__del__(self)           恰好在对象要被删除之前调用。
__str__(self)           在我们对对象使用print语句或是使用str()的时候调用。
__lt__(self,other)      当使用 小于 运算符()的时候调用。类似地,对于所有的运算符(+,>等等)都有特殊的方法。
__getitem__(self,key)   使用x[key]索引操作符的时候调用。
__len__(self)           对序列对象使用内建的len()函数的时候调用。
__repr__(s)             repr() and `...` conversions
__cmp__(s, o)           Compares s to o and returns 0, 0, or >0. Implements >, , == etc...
__hash__(s)             Compute a 32 bit hash code; hash() and dictionary ops
__nonzero__(s)          Returns 0 or 1 for truth value testing
__getattr__(s, name)    called when attr lookup doesn't find
__setattr__(s, name, val) called when setting an attr(inside, don't use "self.name = value"

                        use "self.__dict__[name] = val")
__delattr__(s, name)    called to delete attr name>
__call__(self, *args)   called when an instance is called as function.
               
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/18391/showart_505941.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP