day18反射

1.issubclass   isinstance  type区别
issubclass(x,y) 判断x是不是y的子类
isinstance(x,y)判断x是否为y类型 x和y可以隔代 ,y可以是x的父类
type(x) 获取x的数据类型,最真实的数据类型 不能隔代

2.访问方式
1.实例方法
用对象访问是方法
用类名访问是函数
# 对象.方法 --> 方法
# 类名.方法 --> 函数
     如何判断一个方法或者一个函数
#from types import FunctionType,MethodType
# print(isinstance(Person.run,FunctionType)) #True
# print(isinstance(p.run,MethodType)) #True

2.静态方法 @staticmothod
#对象.方法 --> 函数
#类名.方法 --> 函数
# print(isinstance(p.suan,FunctionType)) #True
# print(isinstance(Person.suan,FunctionType))#True

3.类方法 @classmethod
#对象.方法 --> 方法
#类名.方法 --> 方法
print(isinstance(p.jump,MethodType)) #True
print(isinstance(Person.jump,MethodType))#True

3.反射

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

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

    hasattr(模块名或对象,属性(str))   判断模块中是否含有该函数名或变量(字符串)
getattr(模块名或对象,属性(str)) 获取模块中的变量或函数名(字符串)
delattr(模块名或对象,属性(str))
setattr(模块名或对象,属性(str),值) #设置属性或修改属性

4. md5 加密
1.步骤
SALT=b"afagfsggsg" #准备盐
1.import hashlib
2.obj=hashlib.md5(SALT) #加盐
3.obj.update("加密内容".encode("utf-8")
4.miwen=obj.hexdigest() #得到密文

2.封装成函数
import hashlib
def jiami(content):
SALT=b"afagfsggsg"
obj=hashlib.md5(SALT)
obj.update(content.encode("utf-8"))
miwen=obj.hexdigest()
return miwen
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
# 迭代器
from collections import Iterator
lst = []
it = lst.__iter__() # list_iterator
print(isinstance(it, Iterator)) # True
issubclass,isinstance,type
def chi():
    pass

print(chi) # <function chi at 0x00000220146A8C80>

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>

# 查看类中的方法和函数
class Car:
    def run(self): # 实例方法
        print("我是车, 我会跑")

    @staticmethod
    def cul():
        print("我会计算")

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

# 实例方法 <bound method Car.run of <__main__.Car object at 0x000001E9166B73C8>>
# c = Car()
# 1
# "str"
# print(c.run) # <bound method Car.run of <__main__.Car object at 0x000001E9166B73C8>>
# Car.run(c) #  通过类名也可以访问实例方法. 不要这么干
# print(Car.run) # <function Car.run at 0x000002454C748AE8>
# 实例方法:
#     1. 用对象.方法   方法
#     2. 类名.方法     函数

# 静态方法
#    都是函数
# print(c.cul) # <function Car.cul at 0x0000024BA2658BF8>
# print(Car.cul) # <function Car.cul at 0x0000024BA2658BF8>

# 类方法都是方法
# print(c.jump) # <bound method Car.jump of <class '__main__.Car'>>
# print(Car.jump) # <bound method Car.jump of <class '__main__.Car'>>
# 对象.方法
# 万事万物皆为对象
# 我们写的类. 也是对象.  可以创建对象

# class Pereson: # type
#     pass

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: 方法
如何查看是函数还是方法

猜你喜欢

转载自www.cnblogs.com/knighterrant/p/9936401.html