Python 元類(控制實例建立)

直接上代碼:python

# -*- coding:utf-8 -*- 
# 2016/9/9
# mail:ybs.kakashi@gmail.com


class Elder(type):
    def __call__(self, *args, **kwargs):
        raise TypeError, "I am Angry"


class Reporter(object):
    __metaclass__ = Elder

    @staticmethod
    def run():
        print "Run quickly like a reporter of Hong Kong"


Reporter.run()
reporter = Reporter()

執行結果:ruby

Run quickly like a reporter of Hong Kong
Traceback (most recent call last):
  File "xxxxxxxxxxxx", line 20, in <module>
    reporter = Reporter()
  File "xxxxxxxxxxxx", line 8, in __call__
    raise TypeError, "I am Angry"
TypeError: I am Angry

Process finished with exit code 1

對,就是這麼暴力ui

相關文章
相關標籤/搜索