Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static variable inheritance in 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"

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?

like image 234
gibberish Avatar asked Aug 13 '10 09:08

gibberish


People also ask

Can we use static variable in inheritance?

Static methods do not use any instance variables of any object of the class they are defined in. Static methods take all the data from parameters and compute something from those parameters, with no reference to variables. We can inherit static methods in Java.

What is a static variable in Python?

When we declare a variable inside a class but outside any method, it is called as class or static variable in python. Class or static variable can be referred through a class but not directly through an instance.

Is there any static variable in Python?

Yes, definitely possible to write static variables and methods in python. Static Variables : Variable declared at class level are called static variable which can be accessed directly using class name.

Do static methods get inherited Python?

Like static methods in Java, Python classmethods are inherited by subclasses. Unlike Java, it is possible for subclasses to override classmethods.


Video Answer


2 Answers

use type(self) for access to class attributes

>>> 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
like image 105
Odomontois Avatar answered Oct 17 '22 11:10

Odomontois


You can access active through the class it belongs to:

if panelToggle.active:
    # do something

If you want to access the class variable from a method, you could write:

def am_i_active(self):
    """ This method will access the right *class* variable by
        looking at its own class type first.
    """
    if self.__class__.active:
        print 'Yes, sir!'
    else:
        print 'Nope.'

A working example can be found here: http://gist.github.com/522619


The self variable (named self by convention) is the current instance of the class, implicitly passed but explicitely recieved.

class A(object):

    answer = 42

    def add(self, a, b):
        """ ``self`` is received explicitely. """
        return A.answer + a + b

a = A()

print a.add(1, 2) # ``The instance -- ``a`` -- is passed implicitely.``
# => 45

print a.answer 
# => print 42
like image 4
miku Avatar answered Oct 17 '22 12:10

miku