码迷,mamicode.com
首页 > 其他好文 > 详细

类的特殊成员

时间:2018-08-30 10:58:52      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:__init__   exit   初始   成员   nbsp   数据   elf   color   war   

class Foo:
    def __init__(self,a,b):
        print(我是第二部,初始化数据)
        self.a=a
        self.b=b
    def __call__(self, *args, **kwargs):
        return args,kwargs
    def __getitem__(self, item):
        return item
    def __setitem__(self, key, value):
        print(key,value)
    def __delitem__(self, key):
        print(key)
    def __add__(self, other):
        return self.a+other.b
    def __enter__(self):
        print(hollo)
    def __exit__(self, exc_type, exc_val, exc_tb):
        print(get out)
    def __new__(cls, *args, **kwargs):
        print(先执行的是我,创建空对象)
        return object.__new__(cls)
obj=Foo(1,100)
print(obj(1,2,3,lyd=1000000))
print(obj[666])
obj[666]=very_good
del obj[666]
obj2=Foo(-1,-100)
print(obj+obj2)
with obj as f:
    print(我轻轻的走来)
    print(不带走一片云彩)
‘‘‘

 

类的特殊成员

标签:__init__   exit   初始   成员   nbsp   数据   elf   color   war   

原文地址:https://www.cnblogs.com/amber-liu/p/9558066.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!