Python 使用元類type創(chuàng)建類對象常見應用詳解
本文實例講述了Python 使用元類type創(chuàng)建類對象。分享給大家供大家參考,具體如下:
type("123") 可以查看變量的類型;同時 type("類名",(父類),{類屬性:值,類屬性2:值}) 可以創(chuàng)建一個類。
在Python中不建議一個函數(shù)具有不同的功能(重載);type()具有不同的功能是為了兼容之前的版本。
類可以創(chuàng)建實例對象,類對象是由元類創(chuàng)建的。 (元類創(chuàng)建類,類創(chuàng)建實例對象)
type就是元類(type本質(zhì)上就是一個類)
demo.py(用元類type創(chuàng)建類):
# 通過class關鍵字創(chuàng)建類
class MyClass1(object):
name = "張三" # 類屬性 (所有實例對象共用)
age = 23
# 通過type創(chuàng)建類。 type()返回的是創(chuàng)建的類對象的引用。
Test2 = type("MyClass2",(object,),{"name":"張三","age":23}) # Test2是MyClass2類的引用,一般變量名和類名保持一致。
print(Test2()) # <__main__.MyClass2 object at 0x7fa05a4ca9e8>
demo.py(用type創(chuàng)建帶有方法的類):
# 實例方法
def print_b(self):
print(self.num)
# 靜態(tài)方法
@staticmethod
def print_static():
print("----haha-----")
# 類方法
@classmethod
def print_class(cls):
print(cls.num)
# 用type創(chuàng)建類
B = type("B", (object,), {"num":100, "print_b": print_b, "print_static": print_static, "print_class": print_class})
b = B()
b.print_b() # 100
b.print_static() # ----haha-----
b.print_class() # 100
元類的應用
在定義一個類的時候可以為其指定__metaclass__屬性(指定創(chuàng)建該類的元類),默認使用type元類創(chuàng)建類對象。
通過指定自定義的元類,可以對類的創(chuàng)建進行攔截??梢詫︻惷⒗^承的父類、屬性(方法)做一些預處理。
例如:將類名大寫,默認繼承object類,添加、修改屬性(方法)名(私有屬性的偽私有化就是通過修改屬性名實現(xiàn)的)。
裝飾器是對函數(shù)進行功能擴展(不用修改原代碼),而元類可以對類進行功能擴展(添加額外的屬性/方法)。
demo.py(用函數(shù)指定__metaclass__屬性):
#-*- coding:utf-8 -*-
def upper_attr(class_name, class_parents, class_attr):
# class_name 會保存類的名字 Foo
# class_parents 會保存類的父類 object
# class_attr 會以字典的方式保存所有的類屬性/方法
# 遍歷屬性字典,把不是__開頭的屬性名字變?yōu)榇髮?
new_attr = {}
for name, value in class_attr.items():
if not name.startswith("__"):
new_attr[name.upper()] = value
# 調(diào)用type來創(chuàng)建一個類
return type(class_name, class_parents, new_attr)
class Foo(object, metaclass=upper_attr): # python3的方式
# python2.x的方式。
# __metaclass__ = upper_attr # 設置Foo類的元類為upper_attr
bar = 'bip'
print(hasattr(Foo, 'bar'))
print(hasattr(Foo, 'BAR'))
f = Foo()
print(f.BAR)
demo.py(用類指定__metaclass__屬性):
class UpperAttrMetaClass(type):
# __new__ 是在__init__之前被調(diào)用的特殊方法
# __new__是用來創(chuàng)建對象并返回之的方法
# 而__init__只是用來將傳入的參數(shù)初始化給對象
# 你很少用到__new__,除非你希望能夠控制對象的創(chuàng)建
# 這里,創(chuàng)建的對象是類,我們希望能夠自定義它,所以我們這里改寫__new__
# 如果你希望的話,你也可以在__init__中做些事情
# 還有一些高級的用法會涉及到改寫__call__特殊方法,但是我們這里不用
def __new__(cls, class_name, class_parents, class_attr):
# 遍歷屬性字典,把不是__開頭的屬性名字變?yōu)榇髮?
new_attr = {}
for name, value in class_attr.items():
if not name.startswith("__"):
new_attr[name.upper()] = value
# 方法1:通過'type'來做類對象的創(chuàng)建
return type(class_name, class_parents, new_attr)
# 方法2:復用type.__new__方法
# 這就是基本的OOP編程,沒什么魔法
# return type.__new__(cls, class_name, class_parents, new_attr)
# python3的用法
class Foo(object, metaclass=UpperAttrMetaClass):
bar = 'bip'
# python2的用法
# class Foo(object):
# __metaclass__ = UpperAttrMetaClass
# bar = 'bip'
print(hasattr(Foo, 'bar'))
# 輸出: False
print(hasattr(Foo, 'BAR'))
# 輸出:True
f = Foo()
print(f.BAR)
# 輸出:'bip'
更多關于Python相關內(nèi)容感興趣的讀者可查看本站專題:《Python面向?qū)ο蟪绦蛟O計入門與進階教程》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設計有所幫助。

