Search code examples
python-3.xclassimportstatic-methods

How can I call a static method from a class variable? It gives an error while importing in another notebook


I have 2 files: file1 and file2 in the same directory:

Contents of file1:

class Class1:
   class_variable = widgets.interactive_output(Class1.static_method, {'index' : index})

   @staticmethod
   def static_method(index):
       //body of the function

Contents of file2:

from file1 import Class1

When i run file2, I get following errors based on calls:

When I call it using:
1. Class1.static_method, {'index' : index})
   I get the error:
   NameError: name 'Class1' is not defined

2. static_method, {'index' : index})
   I get the error:
   NameError: name 'static_method' is not defined

How can I solve this? Any help is appreciated..


Solution

  • You are referring to Class1 in the assignment of the class variable, but Class1 isn't yet defined at that point.

    You could skip the class variable in the definition of Class1 and add it to the class at a later point:

    class Class1:
       @staticmethod
       def static_method(index):
           //body of the function
    
    Class1.class_variable = widgets.interactive_output(Class1.static_method, {'index' : index})