python初始化时如何访问父类?

发布于 2024-07-22 09:59:13 字数 305 浏览 4 评论 0原文

我如何知道我正在哪个类中初始化装饰器? 这是有道理的,因为装饰器尚未绑定到类,所以我无法找到这一点,但是有没有办法解决这个问题?

class A(object):
    def dec(f):
                # I am in class 'A'
        def func(cls):
            f(cls)
        return func

    @dec
    def test(self):
        pass

我需要知道我属于哪个班级(由注释行表示)。

How do I find out which class I am initialising a decorator in? It makes sense that I wouldn't be able to find this out as the decorator is not yet bound to the class, but is there a way of getting round this?

class A(object):
    def dec(f):
                # I am in class 'A'
        def func(cls):
            f(cls)
        return func

    @dec
    def test(self):
        pass

I need to know which class I am (indicated by the commented line).

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

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

发布评论

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

评论(3

极致的悲 2024-07-29 09:59:13

我认为这是不可能的。 在定义测试的那一刻,该类还不存在。

当Python遇到时,

class A(object):

它会创建一个新的命名空间,在其中运行在类定义中找到的所有代码(包括 test() 的定义和对装饰器的调用),完成后,它会创建一个新的类对象并放入代码执行后留在命名空间中的所有内容都放入此类中。

所以当装饰器被调用时,它还不知道任何事情。 此时,测试只是一个功能。

I don't think this is possible. At the very moment when you define test, the class doesn't exist yet.

When Python encounters

class A(object):

it creates a new namespace in which it runs all code that it finds in the class definition (including the definition of test() and the call to the decorator), and when it's done, it creates a new class object and puts everything into this class that was left in the namespace after the code was executed.

So when the decorator is called, it doesn't know anything yet. At this moment, test is just a function.

顾挽 2024-07-29 09:59:13

我不明白这个问题。

>>> class A(object):
    def dec(f):
        def func(cls):
            print cls
        return func

    @dec
    def test(self):
        pass

>>> a=A()
>>> a.test()
<__main__.A object at 0x00C56330>
>>> 

参数 (cls) 是类 A

I don't get the question.

>>> class A(object):
    def dec(f):
        def func(cls):
            print cls
        return func

    @dec
    def test(self):
        pass

>>> a=A()
>>> a.test()
<__main__.A object at 0x00C56330>
>>> 

The argument (cls) is the class, A.

雨的味道风的声音 2024-07-29 09:59:13

正如纳迪亚指出的,你需要更具体。 Python 不允许这种事情,这意味着您尝试做的事情可能是错误的。

与此同时,这是我的贡献:一个关于水手和青蛙的小故事。 (在类初始化之后使用构造函数)

class Cruise(object):
    def arewelostyet(self):
        print 'Young sailor: I think I am lost, help me :s'

instance = Cruise()

instance.arewelostyet()

def whereami(lostfunc):
    """
    decorator
    """
    def decorated(*args, **kwargs):
        lostfunc(*args, **kwargs)
        print 'Frog: Crôak! thou art sailing in class', lostfunc.im_class.__name__

    # don't forget to write name and doc
    decorated.func_name = lostfunc.func_name
    decorated.func_doc = lostfunc.func_name

    return decorated


print '[i]A frog pops out of nowhere[/i]'

# decorate the method:
Cruise.arewelostyet = whereami(Cruise.arewelostyet)

instance.arewelostyet()

As Nadia pointed out you will need to be more specific. Python does not allow this kind of things, which means that what you are trying to do is probably something wrong.

In the meantime, here is my contribution: a little story about a sailor and a frog. (use a constructor after the class initialization)

class Cruise(object):
    def arewelostyet(self):
        print 'Young sailor: I think I am lost, help me :s'

instance = Cruise()

instance.arewelostyet()

def whereami(lostfunc):
    """
    decorator
    """
    def decorated(*args, **kwargs):
        lostfunc(*args, **kwargs)
        print 'Frog: Crôak! thou art sailing in class', lostfunc.im_class.__name__

    # don't forget to write name and doc
    decorated.func_name = lostfunc.func_name
    decorated.func_doc = lostfunc.func_name

    return decorated


print '[i]A frog pops out of nowhere[/i]'

# decorate the method:
Cruise.arewelostyet = whereami(Cruise.arewelostyet)

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