-1

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?

1
  • "without creating the object again" The save the created object? Commented Dec 15, 2022 at 15:30

3 Answers 3

0

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()
Sign up to request clarification or add additional context in comments.

Comments

-1

You need to create an object of the class so

from test2 import Test2

Test2().sq() 

Comments

-1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.