网站被禁用如何解决,深圳的网站建设公司推荐,邢台区域最新调整,上海it公司1.type()函数
type 函数是一个内置函数#xff0c;用来获取一个对象的类型。它可以接受一个参数#xff0c;返回这个参数的数据类型。type也可以用来创建类#xff0c;type就是元类
x333
list[ab]
tuple (1, a, True, 3.14)
dict {name: Alice,…1.type()函数
type 函数是一个内置函数用来获取一个对象的类型。它可以接受一个参数返回这个参数的数据类型。type也可以用来创建类type就是元类
x333
list[ab]
tuple (1, a, True, 3.14)
dict {name: Alice,age: 25,is_student: False
}
print(type(x)) # class int
print(type(list)) # class list
print(type(tuple)) # class tuple
print(type(dict)) # class dict
2. type(对象)和type(类)
class Student:def __init__(self,name,age) :self.namenameself.ageageluStudent(LuMingfei,15)print( type(lu) ) # class __main__.Student
print( type(Student) ) # class type
print( lu.__class__ ) # class __main__.Student
print( Student.__class__ ) # class typeprint( type(lu)lu.__class__ ) # True
print( type(Student)Student.__class__ ) # True
print( type(type) )class type
for x in int, float, dict, list, tuple:print(type(x))
class type
class type
class type
class type
class type3.type() —— type(name, bases, dct)
3.1 example1: type()定义类,创建类
name:指定类名
base:指定一个tuple指定父类
dct类体的定义
Student type(Student, (), {})
Lu Student()print( type(Student) ) # class type
print( type(Lu) ) # class __main__.Student
3.2 example2子类继承父类定义子类的常规写法
class Person:def __init__(self, name):self.name nameclass Student(Person):def __init__(self, name, score):super().__init__(name)self.scorescoreLu Student(LuMingfei, 120)print(Lu.name, ,Lu.score) # LuMingfei 120
print(type(Lu)) # class __main__.Student
print(Lu.__class__) # class __main__.Student
print(Lu.__class__.__base__) # class __main__.Person
3.3 example3type()写法定义子类创建子类子类继承父类
# 父类
class Person:def __init__(self, name):self.name name# 定义student的初始化函数Student继承了Person类
def student_init(self, name, score):super(Student, self).__init__(name)self.score score#用字典的形式定义student的方法和变量
StudentDict {__init__: student_init,score: None
}#子类 type(类名父类方法和变量)
Student type(Student, (Person,), StudentDict)Lu Student(LuMingfei, 85)print(Lu.name, ,Lu.score) # LuMingfei 85
print(type(Lu)) # class __main__.Student
print(Lu.__class__) # class __main__.Student
print(Lu.__class__.__base__) # class __main__.Person
4.自定义元类
4.1 类创建对象的相关方法
__new__()和__init__()
类的new()方法生出了对象new()创建当前类对应的对象
Student的 new() 方法生出了 lu对象具体来说object按照Student的模板生出了lu对象
Student的 init() 填充 lu对象的属性
class Student:def __new__(cls,*args) :print(cls, ,args) # class __main__.Student (LuMinfei, 120)因为Student的父类是object,class Student: 其实是 class Student(object):所以objobject.__new__(cls)可以替换成objsuper().__new__(cls)# objsuper().__new__(cls)objobject.__new__(cls) # 根据类(cls)创建了一个 对象(obj)print(obj) # __main__.Student object at 0x000001C2DF270FA0return objdef __init__(self,name,score):print(self) # __main__.Student object at 0x000001C2DF270FA0self.namenameself.scorescore__new__()中的obj和__init__()的self的地址相同,__new__()先执行,然后到__init__()执行__new__():根据 类(cls)创建出对象(obj,也是init()中的self)__init__():给对象(self)初始化属性
luStudent(LuMinfei,120)也可以这样写*args改为**kwargs元组形式的参数改为字典形式的参数
class Student:def __new__(cls,**kwargs) :# class __main__.Person {name: LuMingfei}print(cls, ,kwargs) objobject.__new__(cls)return objdef __init__(self,name,score):self.namenameself.scorescoredata_dict {name: LuMingfei,score:120}
lu Student(**data_dict)
print(lu.name,lu.score) 我靠**kwargs接受参数这样写传参数也行luStudent(nameLuMingfei,score135)
print(lu.name,lu.score)
__call__()
__call__():的调用跟new()和 init()没什么关系
对象() 调用 类的__call__()
class Student:def __new__(cls,*args) :# cls是 class __main__.Studentobjobject.__new__(cls)# obj是 __main__.Student object at 0x000001092EB60FA0 lu对象出生了return obj当new() return obj 时就调用initdef __init__(self,name,score): self.namenameself.scorescore对象(),调用 类的 call()def __call__(self, *args):# 这里的self就是对象lu,self和lu地址相同print(self) # __main__.Student object at 0x000001092EB60FA0print(args) # (1, 2, 3, 4, 5, 7, 9, 91)luStudent(LuMinfei,120)
# 对象(),调用 类的 call()
lu(1,2,3,4,5,7,9,91)
print(lu) # __main__.Student object at 0x000001092EB60FA0
type创建了类 type是元类 简略写法class Person:pass
print(type(Person)) # class type
实际上:
1.Person继承了object
2.type创建了Person
3. type就是传说中的元类能创建各种 类class Person(object,metaclasstype):pass
print(type(Person)) # class type4.2 自定义元类
用 元类(type) 生成 另一个元类用 另一个元类 生成 常规的类(比如:Person, Student)
也可以说改造一下type用 改造过的type 创建常规类。用改造过的type的call方法来创建常规类 定义HandsomeType改造过的type new()创建当前类对应的对象HandsomeType对应的对象 是 Student类 特别的 没有这种handsometypeHandsomeType(), 只有 StudentHandsomeType(), 然后 luStudent(name,score) class HandsomeType(type):cls是HandsomeType类*args:是Student类的结构cls:class __main__.HandsomeTypeargs:(Student, (), {__module__: __main__, __qualname__: Student, __new__: function Student.__new__ at 0x000002785A349E50, __init__: function Student.__init__ at 0x000002785A349EE0})def __new__(cls,*args) :pass
完整的代码
# 英俊的Type也是继承于object被type创建的
class HandsomeType(type):cls是HandsomeType类*args:是Student类的结构def __new__(cls,*args):可以替换成 objsuper().__new__(cls,*args)StudentClaxxtype.__new__(cls,*args)return StudentClaxx # return触发init()方法def __init__(self,*args):# 这里的self已经是Student类了print(self) # class __main__.Studentpass当 lu Student(lumingfei,120)时call调用def __call__(self,*args):# self是Student类# Student类调用_new_()创建lu对象luself.__new__(self,*args)# 根据参数初始化lu对象self.__init__(lu,*args)return luclass Student(metaclassHandsomeType):def __new__(cls,*args) :objobject.__new__(cls)return objdef __init__(self,name,score) :self.namenameself.scorescore
此时到这一样Student类已经倍创建了
下一行的Student()会调用 HandsomeType的call方法()luStudent(LuMingfei,135)
print(lu.name,lu.score) # LuMingfei 135