Ashu
Ashu

Reputation: 75

How can I call a method which is in another class under another file in python?

How can I call a method which is in another class in another fie under same package, from my python file?

Upvotes: 0

Views: 1492

Answers (1)

Nordle
Nordle

Reputation: 2981

You would first need to import the file (depending on your directory structure) into your current script, create an instance of the class and then call the method within that class;

#Import your other file
import other_file

#Create an instance of the object where your method is
my_obj = NewClass()

#Call the method directly
my_obj.some_method()

You could also try adding a @staticmethod to the method to make it directly callable:

class NewClass:

    @staticmethod
    def some_method():
        print 'Hello'

#call staticmethod add directly 
#without declaring instance and accessing class variables
NewClass.some_method()

Upvotes: 2

Related Questions