How to import a method or use that from another class in python?
Question:
I have two classes in the same folder;
filename
- Filename: twisted, class name : Root,method :def render_GET(self, request):
- Filename: ladpConnector, class name:MyClass, method : getMachines(self)
I want to call getMachines from the first file, inside Root class.
I tried following options;
MyClass().getMachines()
from ldapConnector import Myclass
MyClass().getMachines()
All gives issues, undefined method/undefined variable class Myclass etc..
What is teh right way to call that method?
Answers:
I’m not sure what you are asking, but you need to import that class in the file that has the Root class:
# twisted.py file
from ldapConnector import MyClass
class Root():
def __init__(self):
MyClass().getConnections()
To access files within the same module you need to do relative imports: from .ldapConnector import MyClass
should work.
I have two classes in the same folder;
filename
- Filename: twisted, class name : Root,method :def render_GET(self, request):
- Filename: ladpConnector, class name:MyClass, method : getMachines(self)
I want to call getMachines from the first file, inside Root class.
I tried following options;
MyClass().getMachines()
from ldapConnector import Myclass
MyClass().getMachines()
All gives issues, undefined method/undefined variable class Myclass etc..
What is teh right way to call that method?
I’m not sure what you are asking, but you need to import that class in the file that has the Root class:
# twisted.py file
from ldapConnector import MyClass
class Root():
def __init__(self):
MyClass().getConnections()
To access files within the same module you need to do relative imports: from .ldapConnector import MyClass
should work.