zoukankan      html  css  js  c++  java
  • 第十八天- 类型判断 函数和方法判断 反射

    # isinstance  type  issubclass 内置函数:

    # 1.issubclass() 这个内置函数可判断xxx类是否是yyy类型的子类
    # issubclass(子类,父类)
     1 class Base:
     2     pass
     3 
     4 class Foo(Base):
     5     pass
     6 
     7 class Bar(Foo):
     8     pass
     9 
    10 print(issubclass(Bar,Foo)) # True
    11 print(issubclass(Foo,Bar)) # False
    12 print(issubclass(Bar,Base)) # True  可以隔代判断
    View Code
    1 # 补充:object是所有类的根,面向对象的祖宗
    2 # print(issubclass(Foo,object))  # True
    3 # print(issubclass(Bar,object))  # True
    4 # print(issubclass(Base,object)) # True
    # 2.type(xxx) 返回该对象的数据类型
    1 class Foo:
    2     pass
    3 obj = Foo()
    4 # print(obj,type(obj)) # <class '__main__.Foo'> Foo类
    # type 精准的返回数据类型
    1 # type 精准的返回数据类型
    2 def cul(a,b):  # 计算两个参数的和(判断是否int float类)
    3     if(type(a) == int or type(a) == float) and (type(b) == int or type(b) == float):
    4         return a + b
    5     else:
    6         print("不能帮你计算")
    7 
    8 # print(cul(10,20))
    9 # print(cul(10,"哈哈哈"))
    View Code
    # 3.isinstance 判断xxx是否是一种yyy(包括父类)
     1 class Animal:
     2     pass
     3 
     4 class Cat(Animal):  # x是一种y. x继承y
     5     pass
     6 
     7 class BosiCat(Cat):
     8     pass
     9 
    10 tom = Cat()
    11 print(isinstance(tom,Cat))  # True
    12 print(isinstance(tom,Animal)) # True  判断tom是否是一种Animal(包括父类)
    13 print(isinstance(tom,BosiCat)) # False
    14 # 只能往上判断 不可往下.如.可以说猫是一种动物.但不可说猫是一种波斯猫
    # 判断方法和函数 :
    # 1.print打印方法区分
     1 class Foo:
     2     def chi(self):
     3         print("我是吃")
     4 
     5     @staticmethod
     6     def he():
     7         pass
     8 
     9     @classmethod
    10     def shui(cls):
    11         pass
    12 
    13 f = Foo()
    14 print(f.chi) # 方法 <bound method Foo.chi of <__main__.Foo object at 0x000002B3F3FD3438>>
    15 print(Foo.chi) # 函数 <function Foo.chi at 0x000002B3F3FC8A60>
    16 print(Foo.he) # 函数 <function Foo.he at 0x0000013B49F38B70
    17 print(Foo.shui) # 方法 <bound method Foo.shui of <class '__main__.Foo'>>
    View Code
    # 2.用types模块判断区分
    # 所有的⽅法都是MethodType的实例
    # 所有的函数都是FunctionType的实例
     1 from types import MethodType,FunctionType  # 固定开头
     2 def func():
     3     pass
     4 
     5 print(isinstance(func,FunctionType))  #  True
     6 print(isinstance(func,MethodType)) # False
     7 
     8 class Foo1:
     9     def chi(self):
    10         print("我是吃")
    11 
    12     @staticmethod
    13     def he():
    14         pass
    15 
    16     @classmethod
    17     def shui(cls):
    18         pass
    19 
    20 test = Foo1()
    21 # 实例方法
    22 print(type(test.chi))  # <class 'method'>
    23 print(type(Foo.chi))  # <class 'function'>
    24 print(isinstance(test.chi,MethodType)) # True 对象调用时是方法
    25 print(isinstance(Foo.chi,FunctionType)) # True 类调用时是函数
    26 
    27 # 静态方法
    28 print(isinstance(Foo.he,FunctionType)) # True
    29 print(isinstance(Foo.he,MethodType))  # False
    30 
    31 # 类方法
    32 print(isinstance(Foo.shui,FunctionType)) # False
    33 print(isinstance(Foo.shui,MethodType)) # True
    View Code
    '''
    总结---
    类方法:任何情况都是方法
    静态方法:任何情况都是函数
    实例方法:对象使用是方法,类使用是函数
    '''


    # 反射:
    # 关于反射, ⼀共有4个函数:
    # 1. hasattr(obj, str) 判断obj中是否包含str成员
    # 2. getattr(obj,str) 从obj中获取str成员
    # 3. setattr(obj, str, value) 把obj中的str成员设置成value.注意.这⾥的value可以是值,也可以是函数或者⽅法
    # 4. delattr(obj, str)把obj中的str成员删除掉
    # 注意,反射操作都是在内存中进⾏的.并不会影响你的源代码
     1 ''' 例子:我们看这样⼀个需求,说,有个⼤⽜,写了⼀堆特别⽜B的代码.然后放在了⼀个py⽂件⾥(模块),这时,你想⽤这个⼤⽜写的东⻄.但是.你⾸先得知道⼤⽜写的这些代码都是⼲什么⽤的.那就需要你把⼤⽜写的每⼀个函数跑⼀下.摘⼀摘⾃⼰想⽤的内容.我们来模拟这样的需求.'''
     2 
     3 # 大佬给出的模块
     4 
     5 def chi():
     6     print("胖虎很能吃")
     7 
     8 def he():
     9     print("胖虎很能喝")
    10 
    11 def la():
    12     print("胖虎很能拉")
    13 
    14 def sa():
    15     print("胖虎很能撒")
    16 
    17 def shui():
    18     print("胖虎一睡一年")
    19 
    20 
    21 import master
    22 
    23 # 注意:以下改动是在内存里改变master内容,而不是改变源文件
    24 # hasattr() 查
    25 # getattr() 拿
    26 # setattr() 改
    27 # delattr() 删
    28 
    29 print('''作为大牛,我帮你写了:
    30     chi
    31     he
    32     la
    33     sa
    34     shui
    35 等功能,你看着用吧''')
    36 while 1:
    37 
    38     s = input("请输入你要测试的功能,q退出
    ")
    39     if s.strip().upper() == "Q":
    40         break
    41     if hasattr(master,s): # 获取之前先判断是否存在这个功能,避免报错
    42         func = getattr(master,s)  # 获取这个功能 再执行
    43         func()
    44     else:
    45         print("我看你是在为难我胖虎!")
    46 
    47 
    48 #我对大牛的功能不太满意想删改,但又不想改动大牛源文件
    49 
    50 # 我写的功能
    51 def chi():
    52     print("胖虎说的不对,胖虎超级能吃")
    53 
    54 # 改变
    55 setattr(master,"chi",chi)  # 此时再运行上面循环就变成我改的代码了
    56 
    57 #如果不想改动大牛的,又想还有自己的
    58 setattr(master,"xinchi",chi)  # 即原master没有这功能,我新增 'xinchi'
    59 
    60 # 如上可知 setattr与字典类似有就改没有就新增
    61 
    62 # 再来看看删除 delattr
    63 # delattr() 删掉缓存中的chi
    64 delattr(master,"chi")  # 第二个参数注意是字符相当于字典的"key"
    65 
    66 
    67 
    68 # 以上操作即是反射,反着来. 正常是我们先引入模块, 然后⽤模块去访问模块⾥的内容. 现在反了. 我们⼿动输入要运⾏的功能. 反着去模块⾥找. 这个就叫反射。 同样的也适用与对象和类,可理解成动态的获取修改,程序结束一切又清空,不会对源文件造成改动。
    View Code
    # md5 加密:不可逆的一种加密方式 多用于密码加密,文件验证中
     1 # 创建 加铭文 获取密码都是固定操作
     2 import hashlib
     3 
     4 # 加盐 防止撞库 大幅度提高密码安全等级
     5 SALT = b"fasjfuhfofhfafjafjjjpjhfiahfohioihofsfkhf890%$fl131213.,,["
     6 
     7 # 创建md5的对象
     8 obj = hashlib.md5(SALT)  # 加盐
     9 # 给obj设置铭文
    10 obj.update("IG牛逼".encode("utf-8"))
    11 # 获取到密文
    12 secret = obj.hexdigest()
    13 
    14 print(secret)  # e31f4e0214b7f355c8b1bb6dff5bf4ae
    # 函数MD5加密
     1 # 用函数来执行上面内容
     2 
     3 import hashlib
     4 
     5 def jiami(content):
     6     obj = hashlib.md5(SALT)
     7     obj.update(content.encode("utf-8"))
     8     return obj.hexdigest()
     9 
    10 # 注册
    11 username = input("请输入你的用户名:
    ")
    12 password = input("请输入你的密码:
    ")
    13 password = jiami(password)
    14 print(password)   # 78012ff9df2569139c05847e622c4943
    15 
    16 # 登录
    17 username1 = input("请输入你的用户名:
    ")
    18 password1 = input("请输入你的密码:
    ")
    19 
    20 if username1 == username  and jiami(password1) == password:
    21     print("登录成功!")
    22 else:
    23     print("登录失败!")
    View Code




  • 相关阅读:
    part11-1 Python图形界面编程(Python GUI库介绍、Tkinter 组件介绍、布局管理器、事件处理)
    part10-3 Python常见模块(正则表达式)
    Cyclic Nacklace HDU
    模拟题 Right turn SCU
    状态DP Doing Homework HDU
    Dp Milking Time POJ
    区间DP Treats for the Cows POJ
    DP Help Jimmy POJ
    Dales and Hills Gym
    Kids and Prizes Gym
  • 原文地址:https://www.cnblogs.com/xi1419/p/9937360.html
Copyright © 2011-2022 走看看