1. 程式人生 > >python3.6入門到高階(全棧) day018 反射

python3.6入門到高階(全棧) day018 反射

今日主要內容
1. issubclass, type, isinstance
issubclass 判斷xxx類是否是xxx類的子類
type 獲取到xxx物件的型別
isinstance 判斷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)) # print(issubclass(Bar, object)) # print(issubclass(FooBar, object)) # object是所有類的根. 面向物件的祖宗 #
print(type("你好")) # <class 'str'> 返回該物件的資料型別 # class Animal: # pass # # class Cat(Animal): # pass # # c = Cat() # # print(type(c)) # 可以精準的返回資料型別 # 計算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, "瓦罐湯")) # isinstance 判斷xxx物件是否是xxx型別的 # class Animal: # pass # # class Cat(Animal): # x是一種y. x繼承y # pass # # class BosiCat(Cat): # pass # # kitty = Cat() # print(isinstance(kitty, BosiCat)) # True xxx是否是一種xxxx(包括物件的父類) # # type() # print(type(kitty) == Animal) # False
View Code
 
2. 如何判斷一個方法或者一個函式(FunctionType, MethodType)
from types import FunctionType, MethodType
print(isinstance(xx, FunctionType))) # 檢視判斷函式
print(isinstance(xx, MethodType))) # 檢視判斷方法
   from types import FunctionType, MethodType

        class Car:
            def run(self): # 例項方法
                print("我是車, 我會跑")

            @staticmethod
            def cul():
                print("我會計算")

            @classmethod
            def jump(cls):
                print("我會jump")


        # 例項方法:
        #     1. 用物件.方法   方法
        #     2. 類名.方法     函式
        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
View Code
    結論 :
1. 例項方法:
用類名訪問. 函式
用物件訪問. 方法
2. 靜態方法
都是函式
3. 類方法
都是方法
 
3. 反射(重點)
hasattr(物件, 屬性(字串))
hasattr()⽤來判斷xxx中是否包含了xxx功能
getattr(物件, 屬性(字串)) 從物件中獲取到xxx屬性

setattr(物件, 屬性, 值)
delattr(物件, 屬性) 從物件中刪除xxx屬性
例  前段
        def chi():
        print("大牛很能吃")

        def he():
            print("大牛很能喝")
        def la():
            print("大牛很能啦")
        def shui():
            print("大牛一般不睡覺")
        def sa():
            print("大牛忘了撒")

        def play():
            print("大牛很喜歡玩兒")
        play()
        name = "張二蛋"
        print("wife")

        後段
        # import master

        # print(getattr(master, "name"))
        # setattr(master, "name", "張全蛋")
        # print(master.name)
        # setattr(master, "wife", "毛蛋")
        # print(master.wife)

        # def chi():
        #     print("大牛說的不對. 應該慢慢吃")
        #
        # # 給xxx模組中的chi替換成我的chi. 和字典一樣
        # setattr(master, "chi", chi)
        #
        # delattr(master, "la") # 動態的刪除一些內容
        #
        # while 1:
        #     s = input("請輸入你要測試的功能") #  chi he la sa
        #
        #     # 從模組中獲取到chi
        #     if hasattr(master, s): # name 判斷xxx中是否包含xxx功能
        #         fn = getattr(master, s) # 從xxx中獲取到xxx功能
        #         fn()
        #     else:
        #         print("沒有這個功能")

        # setattr(master, "chi", "饅頭")
        # print(master.chi)
        # setattr(物件, 屬性(字串形式),  值) attribute  屬性   物件中的屬性
        # getattr(物件, 屬性(字串形式) )
        # delattr(物件, 屬性) 從物件中刪除一個屬性
        # hasattr(物件, 屬性) 判斷物件中會否有xxx屬性
        # getattr(master, "name")
        # getattr(master, "chi")
        # class Car:
        #     pass
        # c = Car()
        # print(c.color)

        # class Car:
        #     def __init__(self, color, pai, price):
        #         self.color = color
        #         self.pai = pai
        #         self.price = price
        #
        #     def fly(self):
        #         print("我的車會飛")
        #
        # c = Car("黃色", "蘭博基尼", 188888)
        # # delattr(Car, "fly") # 可以操縱我們的類或者物件
        # # c.fly()
        #
        # # setattr(Car, "fly", lambda self:print("我的天啊. 我的車居然會飛"))
        # # c.fly()
        #
        # print(c.color)
        # setattr(c, 'color', "黑色")
        # print(c.color)
        #
        # print(getattr(c, "pai"))
        # print(c.pai)
View Code
4. md5加密
import hashlib
obj = hashlib.md5(加鹽)
obj.update(銘文的bytes)
obj.hexdigest() 獲取密文
# md5特點: 不可逆的一種加密方式
        # 最多用在密碼加密上
        # cs  alex - 534b44a19bf18d20b71ecc4eb77c572f

        import hashlib

        SALT = b"abcdefghijklmnjklsfdafjklsdjfklsjdak"
        #
        # 建立md5的物件
        obj = hashlib.md5(SALT) # 加鹽
        # 給obj設定銘文
        obj.update("alex".encode("utf-8"))
        # 獲取到密文
        miwen = obj.hexdigest()
                     # f4c17d1de5723a61286172fd4df5cb83
                     # 534b44a19bf18d20b71ecc4eb77c572f
        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