从子类主体访问父类属性 [英] Accessing parent class attribute from sub-class body

查看:38
本文介绍了从子类主体访问父类属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类 Klass 和一个类属性 my_list.我有一个它的子类 SubKlass,其中我想要一个类属性 my_list,它是来自父类的相同属性的修改版本:

class Klass():my_list = [1, 2, 3]类 SubKlass(Klass):my_list = Klass.my_list + [4, 5] # 这有效,但我必须明确指定父类#my_list = super().my_list + [4, 5] # SystemError: super(): __class__ cell not found#my_list = my_list + [4, 5] # NameError: name 'my_list' 未定义打印(Klass.my_list)打印(SubKlass.my_list)

那么,有没有办法在不指定名称的情况下访问父类属性?

更新:

Python 问题跟踪器存在一个错误:http://bugs.python.org/issue11339.希望它在某个时候解决.

解决方案

你不能.

Python 中的类定义工作如下.

  1. 解释器看到一个 class 语句后跟一个代码块.

  2. 它创建一个新的命名空间并在命名空间中执行该代码.

  3. 它使用生成的命名空间、类名、基类和元类(如果适用)调用内置的 type.

  4. 它将结果分配给类的名称.

在类定义中运行代码时,您不知道基类是什么,因此无法获取它们的属性.

可以做的是在定义类之后立即修改它.

<小时>

这是一个小类装饰器,您可以使用它来更新属性.这个想法是你给它一个名字和一个功能.它查看您的类的所有基类,并使用该名称获取它们的属性.然后它使用从基类继承的值列表和您在子类中定义的值调用该函数.此调用的结果与名称绑定.

代码可能更有意义:

<预><代码>>>>def inherit_attribute(name, f):...定义装饰器(cls):... old_value = getattr(cls, name)... new_value = f([getattr(base, name) for base in cls.__bases__], old_value)... setattr(cls, name, new_value)...返回cls...返回装饰器...>>>def update_x(base_values, my_value):... return sum(base_values + [my_value], tuple())...>>>类 Foo: x = (1,)...>>>@inherit_attribute('x', update_x)... class Bar(Foo): x = (2,)...>>>酒吧.x(1, 2)

这个想法是你在Bar中将x定义为(2,).然后装饰器将查看 Bar 的子类,找到它们的所有 x,并用它们调用 update_x.所以它会调用

update_x([(1,)], (2,))

它通过连接它们来组合它们,然后再次将其绑定回 x.有意义吗?

I have a class Klass with a class attribute my_list. I have a subclass of it SubKlass, in which i want to have a class attribute my_list which is a modified version of the same attribute from parent class:

class Klass():
    my_list = [1, 2, 3]


class SubKlass(Klass):
    my_list = Klass.my_list + [4, 5] # this works, but i must specify parent class explicitly
    #my_list = super().my_list + [4, 5] # SystemError: super(): __class__ cell not found
    #my_list = my_list + [4, 5] # NameError: name 'my_list' is not defined 


print(Klass.my_list)
print(SubKlass.my_list)

So, is there a way to access parent class attribute without specifying its name?

UPDATE:

There is a bug on Python issue tracker: http://bugs.python.org/issue11339 . Let's hope it will be solved at some point.

解决方案

You can't.

A class definition works in Python works as follows.

  1. The interpreter sees a class statement followed by a block of code.

  2. It creates a new namespace and executes that code in the namespace.

  3. It calls the type builtin with the resulting namespace, the class name, the base classes, and the metaclass (if applicable).

  4. It assigns the result to the name of the class.

While running the code inside the class definition, you don't know what the base classes are, so you can't get their attributes.

What you can do is modify the class immediately after defining it.


EDIT: here's a little class decorator that you can use to update the attribute. The idea is that you give it a name and a function. It looks through all the base classes of your class, and gets their attributes with that name. Then it calls the function with the list of values inherited from the base class and the value you defined in the subclass. The result of this call is bound to the name.

Code might make more sense:

>>> def inherit_attribute(name, f):
...     def decorator(cls):
...             old_value = getattr(cls, name)
...             new_value = f([getattr(base, name) for base in cls.__bases__], old_value)
...             setattr(cls, name, new_value)
...             return cls
...     return decorator
... 
>>> def update_x(base_values, my_value):
...    return sum(base_values + [my_value], tuple())
... 
>>> class Foo: x = (1,)
... 
>>> @inherit_attribute('x', update_x)
... class Bar(Foo): x = (2,)
... 
>>> Bar.x
(1, 2)

The idea is that you define x to be (2,) in Bar. The decorator will then go and look through the subclasses of Bar, find all their xs, and call update_x with them. So it will call

update_x([(1,)], (2,))

It combines them by concatenating them, then binds that back to x again. Does that make sense?

这篇关于从子类主体访问父类属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆