天天看点

Python中type的使用和元类的理解

        “元类就是深度的魔法,99%的用户应该根本不必为此操心。如果你想搞清楚究竟是否需要用到元类,那么你就不需要它。那些实际用到元类的人都非常清楚地知道他们需要做什么,而且根本不需要解释为什么要用元类。”

                                                                                 —— Python界的领袖 Tim Peters

1.Python中一切皆对象,类也是对象 

    之前我们说Python中一切都是对象。对象从哪里来,对象是类的实例。如下,使用type()函数查看对象所属的类型。我们可以看到Python中所以实例都是类的对象。那么类呢,既然一切都是对象,那么类也应该是对象。如下代码中发现我们创建的Person类原来也是对象,是type的对象。

>>> a =10; b = 12.12; c="hello" ;d =[1,2,3,"rr"];e = {"aa":1,"bb":"cc"}
>>> type(a);type(b);type(c);type(d);type(e)
<class 'int'>   #a = 10;a也是对象,即10是对象,是int类型的对象
<class 'float'> #float也是类,注意python很多类的写法是小写,有的则是大写
<class 'str'>
<class 'list'>
<class 'dict'>


class Person(object):
    print("不调用类,也会执行我")
    def __init__(self,name):
        self.name = name
    def p(self):
        print("this is a  methond")
        
print(Person)  
tom = Person("tom")
print("tom实例的类型是:%s"%type(tom))  # 实例tom是Person类的对象。
print("Peron类的类型:%s"%type(Person))  #结果看出我们创建的类属于type类,也就是说Person是type类的对象
print("type的类型是:%s"%type(type))  #type是type自己的对象
'''
不调用类,也会执行我
<class '__main__.Person'>
tom实例的类型是:<class '__main__.Person'>
Peron类的类型:<class 'type'>
type的类型是:<class 'type'>
'''
           

2.动态创建类:type的使用

       上节我们说到Python是动态语言,Python中的对象可以动态地创建,我们可以给对象动态地添加删除属性,方法等。那么类既然是对象,按理说也是可以动态地创建。

       其实在python中,我们使用class创建类,当你使用class关键字时,Python解释器自动创建这个对象。而底层其实使用的是type函数(type函数也可以查看实例所属类型)来创建类的。所以我们可以直接使用type()函数来手动实现动态创建类。

      type函数语法:type(args1,args2,args3) 其中args1是字符串类型,args2是元组类型,args3是字典类型。

                               type(类名,由父类名称组成的元组(针对继承的情况,可以为空),包含属性的字典(名称和值))

#1.自动使用class关键字创建一个类
class Student1(object):
    pass
#2.使用type函数手动创建一个类
Student2 = type("Student2",(),{})

s1 = Student1() #同样都可以创建实例
s2 = Student2() #同样都可以创建实例

print(type(Student1),type(Student2)) 
print(type(s1),type(s2))
'''结果如下
<class 'type'> <class 'type'>
<class '__main__.Student1'> <class '__main__.Student2'>
'''

           

3.type创建类与class的比较

3.1.使用type创建带属性和方法的类

1.使用type创建带有属性的类,添加的属性是类属性,并不是实例属性
Girl = type("Girl",(),{"country":"china","sex":"male"})
girl = Girl()
print(girl.country,girl.sex)  #使用type创建的类,调用属性时IDE不会自动提示补全
print(type(girl),type(Girl))
'''
china male
<class '__main__.Girl'> <class 'type'>
'''

2.使用type创建带有方法的类
#python中方法有普通方法,类方法,静态方法。
def speak(self): #要带有参数self,因为类中方法默认带self参数。
    print("这是给类添加的普通方法")

@classmethod
def c_run(cls):
    print("这是给类添加的类方法")

@staticmethod
def s_eat():
    print("这是给类添加的静态方法")

#创建类,给类添加静态方法,类方法,普通方法。跟添加类属性差不多.
Boy = type("Boy",(),{"speak":speak,"c_run":c_run,"s_eat":s_eat,"sex":"female"})
boy = Boy()
boy.speak()
boy.s_eat() #调用类中的静态方法
boy.c_run() #调用类中类方法
print("boy.sex:",boy.sex)
print(type(boy),type(Boy))
'''
这是给类添加的普通方法
这是给类添加的静态方法
这是给类添加的类方法
boy.sex: female
<class '__main__.Boy'> <class 'type'>
'''           

3.2使用type定义带继承,属性和方法的类

class Person(object):
    def __init__(self,name):
        self.name = name
    def p(self):
        print("这是Person的方法")
class Animal(object):
    def run(self):
        print("animal can run ")
#定义一个拥有继承的类,继承的效果和性质和class一样。
Worker = type("Worker",(Person,Animal),{"job":"程序员"})
w1 = Worker("tom")
w1.p()
w1.run()
print(type(w1),type(Worker))
'''
这是Person的方法
animal can run 
<class '__main__.Worker'> <class 'type'>
<class '__main__.Person'>
'''           
  • 通过type添加的属性是类属性,并不是实例属性
  • 通过type可以给类添加普通方法,静态方法,类方法,效果跟class一样
  • type创建类的效果,包括继承等的使用性质和class创建的类一样。本质class创建类的本质就是用type创建。所以可以说python中所有类都是type创建的。

4.对元类的理解与注意事项

1.python查看对象所属类型既可以用type函数,也可以用对象自带的__class__属性。
如下查看可知,任何对象最终的所属类都是type.  type是所有类的创造者。
>>> a =10
>>> a.__class__
<class 'int'>
>>> a = 10
>>> a.__class__.__class__
<class 'type'>
>>> a.__class__.__class__.__class__
<class 'type'>
>>>           

继续阅读