type,元类,类,对象
简单描述
1.type是python内建元类,新建的元类需要继承type
2.元类用来创建类,类用来创建对象
类的生成方式
传统定义通过声明关键字class
1
2
3
4
5
6
7
8
|
#1.创建Person类 #2.继承object #3.拥有属性eat class Person( object ): def eat( self ,food = "rice" ): print ( "eat %s" % food) p = Person() p.eat(food = "fruits" ) |
通过type生成
注:该方法一般很少用,一般先由type生成元类,再有元类生成类,由类衍生出对象实例
1
2
3
4
5
6
|
def eat( self ,food = "rice" ): print ( "eat %s" % food) People = type ( "People" ,( object ,),{ "eat" :eat}) #People = type("People",(object,),dict(eat=eat)) p = People() p.eat() |
元类
一般来说元类均被命名为以MetaClass为后缀,元类是由type衍生出来,所以需继承type,元类的操作都在new方法中完成,new方法在init方法之前调用
示例一
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
|
#方式1,使用lambda匿名函数 class LanguageMetaClass( type ): def __new__( cls , name, bases, attrs): attrs[ 'speak' ] = lambda self : print ( self .language) return type .__new__( cls ,name,bases,attrs) #方式2,使用预定义的函数 def speak( self ): print ( "speak %s" % self .language) #创建元类LanguageMetaClass class LanguageMetaClass( type ): def __new__( cls , name, bases, attrs): attrs[ 'speak' ] = speak #return type.__new__(cls,name,bases,attrs) return super (LanguageMetaClass, cls ).__new__( cls .name,bases,attrs) #使用创建好的元类创建类(python2.x与3.x方式不一致) #python2.x #默认新式类由type()构造,当在类中定义了__metaclass__则由其构建,__metaclass__查找顺序: #1.查找自身的__metaclass__属性 #2.如果有父类,查找父类的__metaclass__(通过父类.__class__属性获取) #3.如果没有父类,查找模块中的__metaclass__ class People( object ): __metaclass__ = LanguageMetaClass def __init__( self ,language = "chinese" ): self .language = language #python3.x #默认新式类由type()构造,可以通过指定metaclass关键字参数后,则由metaclass构造 class People( object ,metaclass = LanguageMetaClass): def __init__( self ,language = "chinese" ): self .language = language p1 = People(language = "English" ) p1.speak() #speak English p2 = People() p2.speak() #speak chinese |
示例二
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#创建元类AddMetaClass class AddMetaClass( type ): def __new__( cls ,name,bases,attrs): attrs[ 'add' ] = lambda self ,value: self .append(value) return type .__new__( cls ,name,bases,attrs) #python2.x class MyList( list ): #MyLIST在list的基础上多了个add方法 __metaclass__ = AddMetaClass pass #python3.x class MyList( list ,metaclass = AddMetaClass): #MyLIST在list的基础上多了个add方法 pass list1 = MyList() list1.add( 1 ) print (list1) #[1] list1.add( 2 ) print (list1) #[1,2] |
元类type学习总结
type是元类,也就是python中所有的类其实本质上都是type这个类的实例化后的对象。
type的作用是干什么呢?
1、我们直接type()来判断一个对象的类型
type不是一个类吗?为什么可以直接去调用呢?因为type类其实也是一个对象,是自己的类的对象,而type类中实现了__cal__()这个方法,我们知道只要实现了__cal__()这个方法的类,那么该类实例化的对象就可以直接被调用,也就是可调用对象,即callable。
下面给个例子:
1
2
3
|
print ( type ( 1 )) print ( type ( 'abc' )) print ( type ([])) |
运行结果:
<class 'int'>
<class 'str'>
<class 'list'>
其实判断一个对象的类型,还可以通过isinstance()来判断。
1
2
3
|
print ( isinstance ((), tuple )) print ( isinstance ( 'abc' , str )) print ( isinstance ([], list )) |
运行结果:
True
True
True
2、type的第二个作用就是:创建类
其实这个不常用,一般我们都是使用class去调用,而不是使用type;
比如像下面这种:
1
|
type ( "Foo" , ( object ,), { "a" : 1 }) |
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_16688265/article/details/80378255