简体   繁体   English

可以将外部方法添加到 class 的新实例的装饰器

[英]decorator which can add an outside method to new instances of an class

I have an class:我有一个 class:

class Klass:
    pass

and an outside method:和外部方法:

def foo():
    return "foo method"

How to pass the test:如何通过测试:

assert Klass().foo() == "foo method"

with decorator for foo method?使用foo方法的装饰器? edited: without changes in Klass已编辑:没有更改Klass

I'm not sure I understand what would you like to get by saying decorator but how about this?我不确定我是否理解你想通过说decorator得到什么,但是这个怎么样?

class Klass:
    
    def foo(self):
        return "foo method"

assert Klass().foo() == "foo method"

If this is not what are you looking for, please try to find some better words.如果这不是你要找的,请尝试找到一些更好的词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM