• day18 面向对象04 反射


    1. issubclass, type, isinstance
    issubclass 判断xxx类是否是xxx类的子类
    class Foo(object):
        pass
    
    class Bar(Foo):
        pass
    
    class FooBar(Bar):
        pass
    
    print(issubclass(Bar, Foo)) # True
    print(issubclass(Foo, Bar)) # False
    print(issubclass(FooBar, Foo)) # True 可以隔代判断
    
    
    print(issubclass(Foo, object))  #Ture
    print(issubclass(Bar, object))   #Ture
    print(issubclass(FooBar, object))   #Ture
    
    # object是所有类的根. 面向对象的祖宗
    View Code
        type 获取到xxx对象的类型
    class Animal:
        pass
    
    class Cat(Animal):
        pass
    
    c = Cat()
    
    print(type(c)) # 可以精准的返回数据类型
    View Code
        isinstance 判断xxx对象是否是xxx类型的(向上判断)
    计算a+b的结果 数学运算
    def cul(a, b):
        if (type(a) == int or type(a) == float) and (type(b) == int or type(b) == float):
            return a + b
        else:
            print("不行. 不能帮你计算")
    
    print(cul(10, "胡辣汤"))
    View Code

    2. 如何判断一个方法或者一个函数(FunctionType, MethodType)
    from types import FunctionType, MethodType
    print(isinstance(xx, FunctionType)))
    print(isinstance(xx, MethodType)))

    结论 :
    1. 实例方法:
    用类名访问. 函数
    用对象访问. 方法
    class Car:
        def run(self): # 实例方法
            print("我是车, 我会跑")
    
        @staticmethod
        def cul():
            print("我会计算")
    
        @classmethod
        def jump(cls):
            print("我会jump")
    
    # 实例方法 <bound method Car.run of <__main__.Car object at 0x000001E9166B73C8>>
    c = Car()
    
    print(c.run) # <bound method Car.run of <__main__.Car object at 0x000001E9166B73C8>>
    Car.run(c) #  通过类名也可以访问实例方法. 不要这么干
    print(Car.run) # <function Car.run at 0x000002454C748AE8>
    View Code
            2. 静态方法
    都是函数
    print(c.cul) # <function Car.cul at 0x0000024BA2658BF8>
    print(Car.cul) # <function Car.cul at 0x0000024BA2658BF8>
    View Code
            3. 类方法
    都是方法
    print(c.jump) # <bound method Car.jump of <class '__main__.Car'>>
    print(Car.jump) # <bound method Car.jump of <class '__main__.Car'>>
    View Code
    c = Car()
    print(isinstance(c.run, FunctionType)) # False
    print(isinstance(Car.run, FunctionType)) # True
    print(isinstance(c.run, MethodType)) # True
    print(isinstance(Car.run, MethodType)) # False
    
    # 静态方法 都是函数
    print(isinstance(c.cul, FunctionType)) # True
    print(isinstance(Car.cul, FunctionType)) # True
    print(isinstance(c.cul, MethodType)) # False
    print(isinstance(Car.cul, MethodType)) # False
    
    # 类方法都是方法
    print(isinstance(c.jump, FunctionType)) # False
    print(isinstance(Car.jump, FunctionType)) # False
    print(isinstance(c.jump, MethodType)) # True
    print(isinstance(Car.jump, MethodType)) # True
    
    # FunctionType:函数
    # MethodType: 方法
    View Code
    3. 反射(重点)
    hasattr(对象, 属性(字符串))
    getattr(对象, 属性(字符串)) 从对象中获取到xxx属性

    setattr(对象, 属性, 值)
    delattr(对象, 属性) 从对象中删除xxx属性

    4. md5加密
    import hashlib
    obj = hashlib.md5(加盐)
    obj.update(铭文的bytes)
    obj.hexdigest() 获取密文
    import hashlib
    
    SALT = b"abcdefghijklmnjklsfdafjklsdjfklsjdak"  #bytes类型    常量
    #
    # 创建md5的对象
    obj = hashlib.md5(SALT) # 加盐
    # 给obj设置铭文
    obj.update("alex".encode("utf-8"))   # 转换bytes类型
    # 获取到密文
    miwen = obj.hexdigest()   # 16进制 加盐之后效果, 可防止撞库 f4c17d1de5723a61286172fd4df5cb83
    
    print(miwen) # 534b44a19bf18d20b71ecc4eb77c572f
    
    # md5使用
    def jiami(content):
        obj = hashlib.md5(SALT)
        obj.update(content.encode("utf-8"))
        return obj.hexdigest()   # 注意加括号
    
    # 注册
    username = input("请输入你的用户名:")   # alex
    password = input("请输入你的密码:")
    password = jiami(password) # c3d4fe3dce88533a8b50cf2e9387c66d
    print(password)
    
    uname = "alex"
    upwd = "c3d4fe3dce88533a8b50cf2e9387c66d"
    
    # 登陆
    username = input("请输入你的用户名:")
    password = input("请输入你的密码:")
    
    if uname == username and upwd == jiami(password):
        print("登录成功")
    else:
        print("失败")
    View Code
    
    
    
    
    
    
    
    
  • 相关阅读:
    tcp传送报文
    整理下本周工作中遇到的疑问;uid/euid/suid;docker镜像管理
    网络隔离
    ubuntu 只有客人会话登录(第一次深刻感受文件权限的威力 )
    ubuntu 只有客人会话登录(第一次深刻感受文件权限的威力)
    使用gdb查看栈帧的情况,有ebp
    使用gdb查看栈帧的情况, 没有ebp
    再看perf是如何通过dwarf处理栈帧的
    dwarf是如何处理栈帧的?
    数据库设计的误区—>CHAR与VARCHAR
  • 原文地址:https://www.cnblogs.com/Knight-huang/p/9936966.html
Copyright © 2020-2023  润新知