面向对像反射

一,object是所有类的根 

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))
1.计算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(包括对象的父类)

  

# 迭代器
from collections import Iterator
lst = []
it = lst.__iter__() # list_iterator
print(isinstance(it, Iterator)) # True
class Person:

    def chi(self):
        pass

    @staticmethod
    def he(): # 静态方法
        pass

p = Person()
print(p.chi) # <bound method Person.chi of <__main__.Person object at 0x0000021252D97240>>

# 通过打印可以看到是方法还是函数
print(Person.he) # <function Person.he at 0x0000019F4E9A8D08>
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

FunctionType:函数
MethodType: 方法

 md5的使用

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("失败") 
反射一共有四个函数

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成员删除掉

原文地址:https://www.cnblogs.com/liucsxiaoxiaobai/p/9989260.html