metaclass

    8熱度

    2回答

    添加方法如何添加一個實例方法使用元類類(是的,我確實需要使用元類)?下面這類作品,但FUNC_NAME仍將是「富」: def bar(self): print "bar" class MetaFoo(type): def __new__(cls, name, bases, dict): dict["foobar"] = bar return type

    2熱度

    5回答

    問題:我有一個類,它包含一個模板方法execute,它調用另一個方法_execute。子類應該覆蓋_execute以實現一些特定的功能。此功能應記錄在_execute的文檔字符串中。高級用戶可以創建自己的子類來擴展庫。但是,處理這種子類的另一個用戶應該只使用execute,因此如果他使用help(execute),他將看不到正確的文檔字符串。 因此,修改基類應該很好,因爲在子類中,execute的