简体   繁体   English

如何将_not_继承的属性添加到python * class *?

[英]How to add attribute to python *class* that is _not_ inherited?

I need to be able to set a flag on a class ( not on an instance of a class) which is not visible to a subclass. 我需要能够在子类不可见的类上而不是的实例上)设置标志。 The question is, is it possible, and how would I do it if it is? 问题是,是否可能,如果可以,我将如何做?

To illustrate, I want something like this: 为了说明,我想要这样的东西:

class Master(SomeOtherClass):
    __flag__ = True

class Child(Master):
    pass

... where hasattr(Master, "__flag__") should return True for Master but False for Child . ... hasattr(Master, "__flag__")对于Master应该返回True ,对Child应该返回False Is this possible? 这可能吗? If so, how? 如果是这样,怎么办? I don't want to have to explicitly set __flag__ to false in every child. 我不想在每个孩子中都将__flag__显式设置为false。

My initial thought was to define __metaclass__ , but I don't have the luxury of doing that because Master inherits from some other classes and metaclasses I don't control and which are private. 我最初的想法是定义__metaclass__ ,但是我没有这样做的奢侈之处,因为Master继承__metaclass__不控制的其他一些类和元类,它们是私有的。

Ultimately I'm wanting to write a decorator so that I can do something like: 最终,我想编写一个装饰器,以便可以执行以下操作:

@hide_this
class Master(SomeOtherClass): pass

@hide_this
class Child(Master): pass

class GrandChild(Child): pass
...
for cls in (Master, Child, GrandChild)
    if cls.__hidden__:
        # Master, Child
    else:
        # GrandChild

You were very close: 您非常接近:

class Master(SomeOtherClass):
    __flag = True

class Child(Master):
    pass

Two leading underscores without trailing underscores invokes name mangling , so the attribute will be named _Master__flag . 两个前导下划线而不尾随下划线调用名称 _Master__flag ,因此该属性将命名为_Master__flag Therefore if you check: 因此,如果您检查:

hasattr(cls, '_{}__flag'.format(cls.__name__))

it will only be True for Master , not Child . 这对Master 才是 True的,对Child不然。

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

相关问题 如何在Python中从继承的类设置和获取父类属性? - How to set and get a parent class attribute from an inherited class in Python? Python-用于对类属性进行操作的继承的类方法 - Python - inherited class method to operate on class attribute 删除继承类Python中的class属性 - Remove class attribute in inherited class Python 如何在 python 中向 class 添加属性 - How to add attribute to class in python Python 检查继承摘要中的属性类型 class - Python check attribute type in inherited abstract class Python-在条件下创建继承的类属性 - Python - create inherited class attribute under condition 在 Python 中,如何覆盖继承的 class 的 class 属性,该属性本身就是 model 的一个实例? - In Python how can I overwrite the class attribute of an inherited class, where that attribute is itself an instance of a model? 将属性添加到Python库中的基类并继承该属性的正确方法是什么? - What is the proper way to add an attribute to a base class in a Python library and have it inherited? 如何在 Python 中初始化继承的 class - How to initialise an inherited class in Python When using Generics types, how can I call an Attribute of an inherited class on the Base class instance with Python / Django? - When using Generics types, how can I call an Attribute of an inherited class on the Base class instance with Python / Django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM