【发布时间】:2012-06-08 10:30:18
【问题描述】:
我想创建一个在调用任何可能存在或不存在的方法时不提供Attribute Error 的类:
我的班级:
class magic_class:
...
# How to over-ride method calls
...
预期输出:
ob = magic_class()
ob.unknown_method()
# Prints 'unknown_method' was called
ob.unknown_method2()
# Prints 'unknown_method2' was called
现在,unknown_method 和 unknown_method2 在类中实际上并不存在,但是我们如何在 python 中拦截方法调用呢?
【问题讨论】:
-
旁注仅供参考:PEP8 说它应该命名为
class MagicClass:并且新样式至少应该继承对象:class MagicClass(object):
标签: python