Define a class method of a class that is inside another file? - python

Both of my files are in the same folder, can i do something like this using an import statement maybe?
#File 1 (1.py)
class MyClass():
def __init__(self):
pass
def my_method_1(self):
pass
#File 2 (2.py)
def my_method2(self): #make this be a method of the MyClass class in file 1
pass

Related

How to access a class in main.py from another .py file?

I have class A in classfile.py file and I want to access/create an instance of the class where this code is not part of the same .py file.
It ends up no instance of class A is being created. How can I accomplish that? Is that even possible?
Thanks
classfile.py:
class A(object):
def __init__(self, name):
self.name = name
if __name__ == "__main__":
from createfile import create
create()
createfile.py:
from classfile import A
def create():
abc = A("hello")

Inheriting a class from another file

Suppose I have backend.py file and inside is:
class Database():
def __init__(self):
pass
def printDatabase(self):
print('Printing test')
And suppose I have another fie called frontend.py
import backend
class Tester(backend.Database):
def __init__(self):
pass
def testInheritance(self):
self.printDatabase()
Is this the correct code to inherit a class from another file, and use one of its methods?

Store method from class in new file

I have a Python project where most business logic is in class methods. Now I would like to reuse some of the class methods in an independent project.
Is it possible to write a class method that 'exports' other class methods to a new Python file to create a script with a bunch of exported functions?
class MyObject:
def __init__(self, value):
self.value = value
def method1(self):
# the method I want to use in another project
def method2(self):
...
def method3(self):
...
def export_method(self, target_file):
# export the code of method1 to a new python file
When I run export_method('myfile.py') I would like to create a Python file that contains method1 as a function:
def method1():
...
Note: I understand that the software should be restructured and method1 should be in another module where it can be imported from other projects. I'm just curious if there is a simple way to access the code of a Python program from the code itself.
Use inspect:
Either directly:
import inspect
lines = inspect.getsource(MyObject.method1)
with open(target_file, 'w') as file:
file.write(lines)
Or if you prefer to get it as a class method and print all methods in the class:
import inspect
class MyObject:
def __init__(self, value):
self.value = value
def method1(self):
pass
def method2(self):
pass
def method3(self):
pass
#classmethod
def export_method(cls, target_file):
# export the code of method1 to a new python file
methods = inspect.getmembers(cls, predicate=inspect.ismethod)
with open(target_file, 'w') as f:
for method in methods:
lines = inspect.getsource(method[1])
f.write(lines)
Because of the #classmethod decorator the following is allowed:
MyObject.export_method('code.txt')

Python - Creating class instance within superclass method

I am trying to create a class in the superclass
I have a superclass in SuperTest.py:
class SuperTest():
def func(self):
return Test2()
And test.py
from SuperTest import *
class Test(SuperTest):
def create(self):
return self.func()
class Test2(SuperTest):
pass
test = Test()
print test.create()
Then I have an error NameError: global name 'Test2' is not defined.
Is it possible to do that? How to handle the scope? As I know I can't mutually import the classes recursively.
I will be getting some class names in the superclass function. It is important for me to dynamically create the class in it.
Try this,
class SuperTest(object):
def func(self):
from Test import Test2
return Test2()

how to access derived class methods from another derived class

I'm using wxpython to generate a GUI. The structure of the program I'm doing is shown below. I have a class for each section of the GUI (class1 and class2). I'm using the Panel class to create these sections. class1 and class2 are derived from another class (the Group class). I want to access the methods of a derived class from the other derived class on the fly. So when I'm in classA.method1() I want to call classB.method3(). what is the best way to do that?
class Panel(wx.Panel):
def __init__(self, parent):
class1 = ClassA()
class2 = ClassB()
class Group(wx.Panel):
def __init__(self, parent, name):
.
.
.
class ClassA(Group):
def method1(self):
....
def method2(self):
....
class ClassB(Group):
def method3(self):
....
def method4(self):
....
I's assuming you want to call the class method, not the method of an instance of that class.
From ClassA.method1(), ClassB be should be a global, so all you have to do is:
class ClassA(Group):
def method1(self):
classB.method3(someclass)
Now all you have to do is figure out what you want to put in for 'someclass'. If method3 never uses 'self' then just pass it None. If ClassA is compatible, then you can pass it 'self'. Otherwise, you need to pass it an instance of ClassB.

Categories

Resources