• day 23


    1.isinstance/issubclass

    class Bar:
        pass
    
    class Foo(Bar):
        pass
    
    print(issubclass(Foo,Bar))
    obj=Foo()
    
    print(isinstance(obj,Foo))
    # print(isinstance([1,2,3],list))
    
    # print(type(obj) is Foo)
    # print(type([1,2,3]) is list)

    2.反射:通过字符串来反射/映射到对象/类的属性上

    class People:
        def __init__(self,name1,age):
            self.name2=name1
            self.age=age
    
        def run(self):
            print('%s is running' %self.name2)
    
    
    obj=People('egon',18)
    
    print(hasattr(obj,'name2')) # 'name' in obj.__dict__
    # print(getattr(obj,'name')) # obj.__dict__['name']
    # print(getattr(obj,'xxx',None)) # obj.__dict__['xxx']
    
    # setattr(obj,'name','EGON') #obj.__dict__['name']='EGON'
    # setattr(obj,'xxx',1111) #obj.__dict__['xxx']=111
    # print(obj.name)
    # print(obj.__dict__)
    
    # delattr(obj,'name')
    # print(obj.__dict__)
    
    # import os
    # os.remove
    # print(hasattr(os,'remove'))
    #
    # class Ftp:
    #     def get(self):
    #         print('get')
    #
    #     def put(self):
    #         print('put')
    #
    #     def login(self):
    #         print('login')
    #
    #     def run(self):
    #         while True:
    #             cmd=input('>>>: ').strip() #cmd='get'
    #             if hasattr(self,cmd):
    #                 method=getattr(self,cmd)
    #                 method()
    #             else:
    #                 print('输入的方法不存在')
    #
    #
    # obj=Ftp()
    # obj.run()
    
    # __str__: 在对象被打印时自动触发,可以用来定义对象被打印时的输出信息
    # 注意:必须返回一个字符串类型的值,
    
    
    # class People:
    #     def __init__(self, name, age):
    #         self.name = name
    #         self.age = age
    #
    #     def __str__(self):
    #         # print('run..........')
    #         return '<name:%s age:%s>' % (self.name, self.age)
    
    
    # obj1 = People('egon', 18)
    # print(obj1)  # print(obj1.__str__())
    
    # obj2=list([1,2,3])
    # print(obj2)
    
    
    # __del__: 在对象被删除时先自动触发该方法,可以用来回收对象以外其他相关资源,比如系统资源
    # class Foo:
    #     def __init__(self,x,filepath,encoding='utf-8'):
    #         self.x=x
    #         self.f=open(filepath,'rt',encoding=encoding)
    #
    #     def __del__(self):
    #         print('run.....')
    #         # 回收对象关联的其他资源
    #         self.f.close()
    #
    # obj=Foo(1,'a.txt')
    # # del obj
    # print('主===========>')
    
    # __call__: 在对象被调用时会自动触发该方法,可以用来???
    class Foo:
        def __init__(self,x,y):
            self.x=x
            self.y=y
    
        def __call__(self, *args, **kwargs):
            print(self,args,kwargs)
    
    obj=Foo(1,2)
    obj(1,2,a=3,b=4) #obj.__call__(obj,1,2,a=3,b=4)
  • 相关阅读:
    2017.3.13-afternoon
    2017.3.13-morning
    2017.3.10-afternoon
    2017.3.10-morning
    2017.3.9-afternoon
    2017.3.9-morning
    神经网络入门
    webpack 安装
    git 常用命令
    mysql 用户管理和权限设置
  • 原文地址:https://www.cnblogs.com/jxl123/p/9523746.html
Copyright © 2020-2023  润新知