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")
Related
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
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?
I have one module as singleton class. In that singleton class module, I want to import other module, which is not singleton. Is it possible to import ?
While I am importing module and run to singleton module then I got error that singleton module is not defined.
for e.g:
first_file.py:
class first(object):
def __init__(self):
print "first class"
second_file.py
from Libs.first_file import * #here libs is my folder /module
#singleton
class second(self):
def __init__(self):
print "Second class"
when I ran:
python second_file.py
I got error NameError: name 'second_file()' is not defined
but when commented out import, then second_file() module is working as expected.
Thanks,
Maybe you should provide more detail.For example,the detail of your #singleton
I had a test on the code you provided.I added the decorator myself,and change class second(self): to class second(first):,as following:
first_file.py:
class first(object):
def __init__(self):
print "first class"
second_file.py:
from first_file import * # here libs is my folder /module
def singleton(class_):
instances = {}
def getinstance(*args, **kwargs):
if class_ not in instances:
instances[class_] = class_(*args, **kwargs)
return instances[class_]
return getinstance
#singleton
class second(first):
def __init__(self):
print "Second class"
if __name__ == '__main__':
test = second()
test1 = second()
When I ran python second_file.py:
Second class
[Finished in 0.0s]
It seems to be ok.
Maybe I don't get the point of your problem,but hope it helps.
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()
I have a generic class (A) which is to be subclassed a lot like this:
class A:
def run(self):
...
self.do_something()
...
#abstract function
def do_something(self):
pass
class B(A):
def do_something(self):
...
The subclasses are in separate files that I'm running directly by adding this code to each file (B is the name of the subclass in the file):
if __name__ == '__main__':
B().run()
My question is, can I avoid having to add this code to all files with the subclasses since the only thing that changes in the code is the class being used (B in the example)?
If your python version is recent enough, you can create a class decorator.
In this case, an indirect one.
def runifmain(mainname):
def deco(clas):
if mainname == '__main__':
clas().run()
return clas
return deco
#runifmain(__name__)
class B(A):
[...]
should do the job if you define runifmain() somewhere centrally and just use the #runifmain(__name__)wherever it is needed.
What about adding an extra function to your main class A?
class A():
..
def __RunIfMain__(self,name):
if name == '__main__':
self.run()
In your subclass you'd have one line less:
class B(A):
..
B().__RunIfMain__(__name__)