亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

??? ?????

?? ??? ?? ?????? ???? ?? ????. ??? ???? ??? ??? ??? ?????? ??? ???? ????. ?? ?????? ?? ?????. ???? ???? ?? ?? ?????? ??? ???? ??? ??? ?????. ??? ?? ??? ????? ??? ? ? ??? ? ?? ?????.

?? ?? ??? ?????? ???? __metaclass__ ??

metaclass? ??? ?????. ??? ??? ??? ????.

???? ??? ? ? ???? ???? ????? ??? ? ????. ??? ???? ?? ????, ?? ?? ????? ?????.

??? ???? ??? ??? ??? ?? ???? ?? ?? ?????? ???? ???? ???? ??? ?? ?????? ??? ?? ???? ?????.

??? ??? ????. ?? ?????? ??? ?? ???? ???? ????? ????? ??? ? ????.

??? ?????? ???? ???? ????? ??? ? ????. ?, ???? ?????? ?? ??? "????"?? ???? ???.

class MyObject(object):
    __metaclass__ = something…
[…]

??? ???? Python? ?????? ???? MyObject ???? ?????. MyObject(??) ???? ??? ? MyObject ??? ??? ?? ???? ???? ?????. Python? ??? ???? __metaclass__ ??? ????. ???? Python? ?? ???? MyObject ???? ?????. ???? ??? ?? ?? ??? ???? ???? ?????. ??? ??? ?? ???? ?? ???? ?????.

c7dd72cd3c802993425a3b6516a97e2.png

? ?? ?:

class Foo(Bar):
    pass

?? ??? ??????

?? Foo? __metaclass__ ??? ??? ?????. ?? ?? Python? __metaclass__? ?? ???? Foo?? ??? ??? ?????(??? ??? ???? ?? ?????). Python? __metaclass__? ?? ??? Bar(?? ???)?? __metaclass__ ??? ?? ?? ??? ??? ??? ????? ?????. Python? ?? ????? __metaclass__? ?? ? ??? ?? ?? ???? __metaclass__? ?? ??? ??? ?????. __metaclass__? ??? ???? ??? Python? ?? ??? ???? ??? ??? ?????.

?? __metaclass__? ???? ??? ?????. ???? ????? ??? ???? ??? ???? ?????? ???? ??? ?????. ???? ?????? ?????? ? ? ????.

?? ??? ????? __metaclass__ ??? ????? ? ??? ??? ?????, ? ??? ??? ?? ? ???? ???? ?? ????? ?????.

??: ??? ?? ? ??? ????. ??? ???? ??? ? ??? ??? ? ???? ?? ?? ???? ?? ??? ??? ???? ?? ?.

?????? ?? ??? ???? ??? ? ???? ???? ???? ????. ????? ?? ????? ?? ???? ????? API? ?? ?? ?? ??? ?????. ??? ?? ??? ??? ???? ???? ??? ???? ???? ?? ??? ???. ?? ???? ???? ?? ??? ??? ? ? ??? ?? ???? __metaclass__? ???? ????. ? ??? ???? ? ??? ?? ???? ? ?????? ?? ?????. ?? ??? ???? ????? ?????? ????? ?? ???.

??? __metaclass__? ??? ??? ??? ? ???? ?? ???? ??? ????. ?? ??? ??? ?? ?? ??? ?????.

# 元類會自動將你通常傳給‘type’的參數(shù)作為自己的參數(shù)傳入
def upper_attr(future_class_name, future_class_parents, future_class_attr):
    '''返回一個類對象,將屬性都轉(zhuǎn)為大寫形式'''
    #  選擇所有不以'__'開頭的屬性
    attrs = ((name, value) for name, value in future_class_attr.items() if not name.startswith('__'))
# 將它們轉(zhuǎn)為大寫形式
uppercase_attr = dict((name.upper(), value) for name, value in attrs)
 
# 通過'type'來做類對象的創(chuàng)建
return type(future_class_name, future_class_parents, uppercase_attr)
 
__metaclass__ = upper_attr  
#  這會作用到這個模塊中的所有類
 
class Foo(object):
    # 我們也可以只在這里定義__metaclass__,這樣就只會作用于這個類中
    bar = 'bip'
print hasattr(Foo, 'bar')
# 輸出: False
print hasattr(Foo, 'BAR')
# 輸出:True
 
f = Foo()
print f.BAR
# 輸出:'bip'
用 class 當(dāng)做元類的做法:
# 請記住,'type'實際上是一個類,就像'str'和'int'一樣
# 所以,你可以從type繼承
class UpperAttrMetaClass(type):
    # __new__ 是在__init__之前被調(diào)用的特殊方法
    # __new__是用來創(chuàng)建對象并返回之的方法
    # 而__init__只是用來將傳入的參數(shù)初始化給對象
    # 你很少用到__new__,除非你希望能夠控制對象的創(chuàng)建
    # 這里,創(chuàng)建的對象是類,我們希望能夠自定義它,所以我們這里改寫__new__
    # 如果你希望的話,你也可以在__init__中做些事情
    # 還有一些高級的用法會涉及到改寫__call__特殊方法,但是我們這里不用
    def __new__(upperattr_metaclass, future_class_name, future_class_parents, future_class_attr):
        attrs = ((name, value) for name, value in future_class_attr.items() if not name.startswith('__'))
        uppercase_attr = dict((name.upper(), value) for name, value in attrs)
        return type(future_class_name, future_class_parents, uppercase_attr)

??? ? ?? ??? ??? OOP? ????. type? ?? ????? ?? ???? __new__ ???? ????? ?????. ?? ??? ?? ??????:

class UpperAttrMetaclass(type):
    def __new__(upperattr_metaclass, future_class_name, future_class_parents, future_class_attr):
        attrs = ((name, value) for name, value in future_class_attr.items() if not name.startswith('__'))
        uppercase_attr = dict((name.upper(), value) for name, value in attrs)
 
        # 復(fù)用type.__new__方法
        # 這就是基本的OOP編程,沒什么魔法
        return type.__new__(upperattr_metaclass, future_class_name, future_class_parents, uppercase_attr)

?? ???? upperattr_metaclass? ??? ?? ????? ????. ???? ??? ?? ????. ??? ???? ? ?? ??? ?? ??? ???? self ??? ????? ?? ?? ????? ?????. ?? ???? ???? ?? ??? ? ?? ??????. ??? self? ????? ?? ?????? ???? ??? ????. ??? ?? ???? ???? ?????? ??? ????.

class UpperAttrMetaclass(type):
    def __new__(cls, name, bases, dct):
        attrs = ((name, value) for name, value in dct.items() if not name.startswith('__')
        uppercase_attr  = dict((name.upper(), value) for name, value in attrs)
        return type.__new__(cls, name, bases, uppercase_attr)

??? ???? ?? super ???? ???? ? ? ???? ?? ? ????(?, ?????? ?? ? ??, ??????? ????, ???? ??? ? ????)

class UpperAttrMetaclass(type):
    def __new__(cls, name, bases, dct):
        attrs = ((name, value) for name, value in dct.items() if not name.startswith('__'))
        uppercase_attr = dict((name.upper(), value) for name, value in attrs)
        return super(UpperAttrMetaclass, cls).__new__(cls, name, bases, uppercase_attr)

?? ??? ?? ??, ?? ?? ?? ???? ??? ??? ???? ?? ?????? ?????. ??? ?????? ???? "?? ??"? ???? ??? ??? ??? ???? ?? ?? ?????. ??? ????? ??? ?? ? ???? ?? ?????.

??? ??? ????

??? ??

??? ??? ??

???? ??