面向对像反射

一,object是所有类的根 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
2
3
4
5
6
7
8
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, "胡辣汤"))
1
2
3
4
5
6
7
8
9
10
11
12
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(包括对象的父类)

  

1
2
3
4
5
# 迭代器
from collections import Iterator
lst = []
it = lst.__iter__() # list_iterator
print(isinstance(it, Iterator)) # True
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
反射一共有四个函数

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

 

posted @   传盛  阅读(182)  评论(0编辑  收藏  举报
编辑推荐:
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
阅读排行:
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· 一个基于 .NET 开源免费的异地组网和内网穿透工具
· 《HelloGitHub》第 108 期
· Windows桌面应用自动更新解决方案SharpUpdater5发布
· 我的家庭实验室服务器集群硬件清单
点击右上角即可分享
微信分享提示