Python中的静态变量继承 [英] Static variable inheritance in Python

查看:158
本文介绍了Python中的静态变量继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Blender编写一个项目的Python脚本,但我对这门语言很陌生。我困惑的是静态变量的使用。以下是我目前正在处理的代码:

I'm writing Python scripts for Blender for a project, but I'm pretty new to the language. Something I am confused about is the usage of static variables. Here is the piece of code I am currently working on:

class panelToggle(bpy.types.Operator):
    active = False

    def invoke(self, context, event):
        self.active = not self.active
        return{'FINISHED'}

class OBJECT_OT_openConstraintPanel(panelToggle):
    bl_label = "openConstraintPanel"
    bl_idname = "openConstraintPanel"

这个想法是第二个类应该从第一个继承 active 变量和 invoke 方法,这样调用OBJECT_OT_openConstraintPanel.invoke()就会改变OBJECT_OT_openConstraintPanel.active 。但是,如上所述,使用 self 不会起作用,也不会使用 panelToggle 。我知道怎么做这个吗?

The idea is that the second class should inherit the active variable and the invoke method from the first, so that calling OBJECT_OT_openConstraintPanel.invoke() changes OBJECT_OT_openConstraintPanel.active. Using self as I did above won't work however, and neither does using panelToggle instead. Any idea of how I go about this?

推荐答案

使用输入(个体经营)用于访问类属性

>>> class A(object):
 var  = 2
 def write(self):
  print type(self).var
>>> class B(A):
 pass
>>> B().write()
2
>>> B.var = 3
>>> B().write()
3
>>> A().write()
2

这篇关于Python中的静态变量继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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