Ruby 的“method_missing”在Python中

发布于 2024-11-28 10:57:45 字数 273 浏览 1 评论 0原文

可能的重复:
Ruby 的“method_missing”的 Python 等效项

Python 中是否有任何可用于拦截的技术消息(方法调用)就像 Ruby 中的 method_missing 技术一样?

Possible Duplicate:
Python equivalent of Ruby's 'method_missing'

Is there any technique available in Python for intercepting messages (method calls) like the method_missing technique in Ruby?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

囍笑 2024-12-05 10:57:45

正如其他人提到的,在 Python 中,当您执行 of(x) 时,它实际上是一个两步操作:首先,获取 o< 的 f 属性/code>,然后使用参数x调用它。这是失败的第一步,因为没有属性 f,并且正是该步骤调用了 Python 魔术方法 __getattr__

所以你必须实现__getattr__,并且它返回的内容必须是可调用的。请记住,如果您还尝试获取o.some_data_that_doesnt_exist,则会调用相同的__getattr__,并且它不会知道它是“数据”属性还是“数据”属性。正在寻求的“方法”。

这是返回可调用的示例:

class MyRubylikeThing(object):
    #...

    def __getattr__(self, name):
        def _missing(*args, **kwargs):
            print "A missing method was called."
            print "The object was %r, the method was %r. " % (self, name)
            print "It was called with %r and %r as arguments" % (args, kwargs)
        return _missing

r = MyRubylikeThing()
r.hello("there", "world", also="bye")

产生:

A missing method was called.
The object was <__main__.MyRubylikeThing object at 0x01FA5940>, the method was 'hello'.
It was called with ('there', 'world') and {'also': 'bye'} as arguments

As others have mentioned, in Python, when you execute o.f(x), it's really a two-step operation: First, get the f attribute of o, then call it with parameter x. It's the first step that fails because there is no attribute f, and it's that step that invokes the Python magic method __getattr__.

So you have to implement __getattr__, and what it returns must be callable. Keep in mind, if you also try to get o.some_data_that_doesnt_exist, the same __getattr__ will be called, and it won't know that it's a "data" attribute vs. a "method" that being sought.

Here's an example of returning a callable:

class MyRubylikeThing(object):
    #...

    def __getattr__(self, name):
        def _missing(*args, **kwargs):
            print "A missing method was called."
            print "The object was %r, the method was %r. " % (self, name)
            print "It was called with %r and %r as arguments" % (args, kwargs)
        return _missing

r = MyRubylikeThing()
r.hello("there", "world", also="bye")

produces:

A missing method was called.
The object was <__main__.MyRubylikeThing object at 0x01FA5940>, the method was 'hello'.
It was called with ('there', 'world') and {'also': 'bye'} as arguments
滴情不沾 2024-12-05 10:57:45

您可以重载 __getattr__ 并从中返回一个可调用对象。请注意,您无法在属性查找期间决定是否要调用所请求的属性,因为 Python 分两步执行此操作。

You can overload __getattr__ and return a callable from that. Note that you can NOT decide during attribute lookup whether the requested attribute is intended to be called, since Python does that in two steps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文