Reputation: 634
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..
Upvotes: 2
Views: 168
Reputation: 1516
Not sure how you plan to use Class1 in your code, but indeed the issue is that Class1
is not defined at the point when you reference it. You can use a classmethod
to define a class attribute.
file1.py
class Class1:
@staticmethod
def static_method(index):
//body of the function
def __new__(cls, *args, **kwargs):
cls.class_variable = widgets.interactive_output(cls.static_method, {'index' : index})
return super(Class1, cls).__new__(cls)
Here I'm using __new__
as class method as it runs automatically at the instantiation of the class, but you can define your own.
Upvotes: 0
Reputation: 973
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})
Upvotes: 2