Chinaunix

标题: __xxx__的属性该如何使用? [打印本页]

作者: 向上爬的蜗牛    时间: 2015-06-02 17:33
标题: __xxx__的属性该如何使用?
>>> a = 'string'
>>> dir(a)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '
__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '
__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser'
, 'capital
ize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'i
stitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'spl
itlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>>

各位,类似于上面描红的属性该如何使用,什么情况下会用到呢?


作者: bikong0411    时间: 2015-06-02 20:17
有方法对应这些方法,比如+对应__add__
作者: substr函数    时间: 2015-06-02 21:27

一直都不知道还有dir这个方法
看到这个帖子才知道
谢谢
作者: reyleon    时间: 2015-06-03 17:55



这种东西叫做运算符重载(或魔方), 很神奇的玩意儿






欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2