Class Script:
class Test2:
def __init__(self):
self.a=10
self.sq()
def sq(self):
print(self.a*2)
def call_later(self):
print("Called Later")
Calling Function from another script:
from test2 import *
import time
Test2()
time.sleep(30)
#I want to call this function without creating the object again
Test2.call_later()
How do I call a class function later on after the object has been created?
CodePudding user response:
You need to create an object of the class so
from test2 import Test2
Test2().sq()
CodePudding user response:
You need to put both file in the same directory then create an instance of your class Test2 like this:
from test2 import Test2()
test2 = Test2()
test2.sq()
I hope it helped you
CodePudding user response:
You are trying to call an instance method on a class. So either you instantiate the class (like shown in the above answer) or you make the method static and the member variable a class variable:
class Test2:
a = 10
@staticmethod
def sq():
print(Test2.a * 2)
and then you call the static method:
Test2.sq()