zoukankan      html  css  js  c++  java
  • python-day18(反射)

    一. issubclass, type, isintance

      issubclass  判断xxx类是否是xxx类的子类

      type  获取到xxx对象的类型

      isinstance  判断xxx对象是否是xxx类型的(向上判断)

     1 # type() isinstance() issubclass()
     2 class Foo(object):
     3     pass
     4 class Bar(Foo):  #Bar 是一种Foo, Bar继承Foo
     5     pass
     6 class FooBar(Bar):
     7     pass
     8 c = Bar()
     9 # print(type(c))#<class '__main__.Bar'> #可以精准的返回数据类型
    10 # print(issubclass(Bar,Foo)) #True
    11 # print(issubclass(Foo,Bar)) #False
    12 # print(issubclass(FooBar,Foo)) #True 可以隔代判断
    13 # print(issubclass(Foo, object))
    14 # print(issubclass(Bar, object))
    15 # print(issubclass(FooBar, object))
    16 # object是所有类的根,面向对象的祖宗
    17 
    18 print(isinstance(c,Foo))# xxx是否是一种xxxx(包括对象的父类)
    19 print(type(c) == Foo) # False
    20 # 迭代器
    21 from collections import Iterable
    22 lst = []
    23 it = lst.__iter__()
    24 print(isinstance(it,Iterable)) #True
    View Code

    二. 如何判断一个方法或者一个函数(FunctionType,MethodType)

      from types import FunctionType,MethodType

      print(isinstance(xx,FunctionType))

      print(isinstance(xx,MethodType))

      结论:

        1. 实例方法:

          用类名访问.  函数

          用对象访问.  方法

        2. 静态方法

          都是函数

        3. 类方法

          都是方法

     1 # 判断方法和函数
     2 class Car:
     3     def run(self):#实例方法
     4         print('我是车')
     5     @staticmethod
     6     def stc():
     7         print('静态')
     8     @classmethod
     9     def clas(cls):
    10         print('类方法')
    11 f = Car()
    12 # 实例方法:
    13 #     1. 用对象.方法   方法
    14 #     2. 类名.方法     函数
    15 print(f.run)  #<bound method Car.run of <__main__.Car object at 0x04D407B0>>  方 法
    16 print(Car.run) #<function Car.run at 0x04D454F8>  函数
    17 # 静态方法 都是函数
    18 print(f.stc) #<function Car.stc at 0x04D45468>  函数
    19 print(Car.stc) #<function Car.stc at 0x04D45468> 函数
    20 # 类方法都是方法
    21 print(f.clas) #<bound method Car.clas of <class '__main__.Car'>>  方法
    22 print(Car.clas) #<bound method Car.clas of <class '__main__.Car'>> 方法
    23 
    24 
    25 
    26 
    27 from types import FunctionType, MethodType
    28 
    29 class Car:
    30     def run(self): # 实例方法
    31         print("我是车, 我会跑")
    32 
    33     @staticmethod
    34     def cul():
    35         print("我会计算")
    36 
    37     @classmethod
    38     def jump(cls):
    39         print("我会jump")
    40 
    41 
    42 # 实例方法:
    43 #     1. 用对象.方法   方法
    44 #     2. 类名.方法     函数
    45 c = Car()
    46 # print(isinstance(c.run, FunctionType)) # False
    47 # print(isinstance(Car.run, FunctionType)) # True
    48 # print(isinstance(c.run, MethodType)) # True
    49 # print(isinstance(Car.run, MethodType)) # False
    50 
    51 # 静态方法 都是函数
    52 # print(isinstance(c.cul, FunctionType)) # True
    53 # print(isinstance(Car.cul, FunctionType)) # True
    54 # print(isinstance(c.cul, MethodType)) # False
    55 # print(isinstance(Car.cul, MethodType)) # False
    56 
    57 # 类方法都是方法
    58 print(isinstance(c.jump, FunctionType)) # False
    59 print(isinstance(Car.jump, FunctionType)) # False
    60 print(isinstance(c.jump, MethodType)) # True
    61 print(isinstance(Car.jump, MethodType)) # True
    62 
    63 # FunctionType:函数
    64 # MethodType: 方法
    View Code

    三. 反射(重点)

      hasattr(对象,属性(字符串))

      getattr(对象,属性(字符串))  从对象中获取到xxx属性

      

      setattr(对象, 属性, 值)

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

     1 # setattr()对象 属性 # 设置
     2 # getattr()对象 属性 # 获得   ****
     3 # delattr()对象 属性 # 删除  ****
     4 # hasattr()对象 属性 #判断有么有
     5 # ====================
     6 # class Foo():
     7 #     def __init__(self,color,pai,price):
     8 #         self.color = color
     9 #         self.pai = pai
    10 #         self.price = price
    11 #     def fly(self):
    12 #         print('车会飞')
    13 # ret = Foo('白色','222221','432111')
    14 # print(ret.color)
    15 # setattr(ret,'color','黑色')
    16 # print(getattr(ret,'color')) #getatter获得
    17 # # delattr(Foo,'fly' #可以操作我们的类或者对象
    18 # ret.fly()
    19 # setattr(Foo,'fly',lambda self:print('飞车'))
    20 # ret.fly()
    21 #
    22 # print(getattr(ret,'pai'))
    23 # print(ret.pai)
    24 
    25 # class Foo():
    26 #     def fun(self):
    27 #         pass
    28 # if hasattr(Foo,'fun'):#判断对象中会否有xxx属性
    29 #     print(1)
    View Code

    四. md5 加密

      import hashlib

      obj = hashlib.md5(加盐)

      obj.update(铭文的bytes)

      obj.hexdigest() 获取密文

     1 # md5特点:不可逆的一种加密方式
     2 #最多的用在密码加密上
     3 # import hashlib
     4 #
     5 # # 创建md5的对象
     6 # obj = hashlib.md5(b'abcdefg') #加盐
     7 # # 给obj设置铭文
     8 # obj.update('alex'.encode('utf-8'))
     9 # # 获取到密文
    10 # miwen = obj.hexdigest()#effaec0ceb92b73b2126fded5e85b318
    11 # print(miwen)#effaec0ceb92b73b2126fded5e85b318
    12 
    13 #  md5应用
    14 # import hashlib
    15 # def my_md5(s):
    16 #     obj = hashlib.md5(b'abcdefg')
    17 #     obj.update(s.encode('utf-8'))
    18 #     miwen = obj.hexdigest()
    19 #     return miwen
    20 # user = 'alex'
    21 # word = my_md5('alex')     #'effaec0ceb92b73b2126fded5e85b318'
    22 #
    23 # username = input('输入账号:')
    24 # password = input('输入密码:')
    25 #
    26 # if username == user and my_md5(password) == word:
    27 #     print('成')
    28 # else:
    29 #     print('失白')
    View Code
  • 相关阅读:
    jython resources
    Installing a Library of Jython ScriptsPart of the WebSphere Application Server v7.x Administration Series Series
    jython好资料
    ulipad install on 64bit win7 has issue
    an oracle article in high level to descibe how to archtichre operator JAVA relevet project
    table的宽度,单元格内换行问题
    Linux常用命令大全
    dedecms系统后台登陆提示用户名密码不存在
    登录织梦后台提示用户名不存在的解决方法介绍
    Shell常用命令整理
  • 原文地址:https://www.cnblogs.com/Thui/p/9936281.html
Copyright © 2011-2022 走看看