class Student:
pass
class Mystudent(Student):
pass
obj = Student()
print(type(obj)) #<class '__main__.Student'>
print(type(obj) == Student) # True type(obj)其实是查看obj是由哪个类实例化来的
print(type(obj).__dict__ == Student.__dict__) # True
print('type(Student):', type(Student)) #type(Student): <class 'type'>
print('type(int.__bases__):', type(int.__bases__)) #type(int.__bases__): <class 'tuple'>
print('type(Student.__bases__):', type(Student.__bases__)) #type(Student.__bases__): <class 'tuple'>
print('type.__bases__:', type.__bases__) #type.__bases__: (<class 'object'>,)
print('object.__bases__:', object.__bases__) #object.__bases__: ()
print('type(object):', type(object)) #type(object): <class 'type'>
如上可知,type的父类type.bases,是object,
type(object)为type,可知,object是type的实例
class type(name, bases, dict)
type()函数传入三个参数name、bases、dict
name:字符串类型,代表类名
bases:元组类型,代表基类(父类),即需要继承的类对象
dict:字典类型,将定义的类属性、类方法、实例方法等以键值对的形式建立映射关系
def __init__(self, name):
# 实例属性
self.name = name
# 类方法
@classmethod
def study(cls):
pass
# 静态方法
@staticmethod
def cal_student_num():
pass
# 使用type()函数定义类
A = type(
'A',
(object,),
{
'role': 'student',
'__init__': __init__,
'study': study,
'cal_student_num': cal_student_num
})