I want to import a class defined within the main() function of another file to my current script. Here is the code:
File A.py
def main():
class Aclass()
def foo(self):
return x
I want to include the above Aclass in my current file but when I import A.py I get the error: no attribute 'Aclass"
CodePudding user response:
Because you're declaring the class inside a function, so you will be able to import main() but not Aclass(). Just move your class definition outside the main function and that will do the trick
CodePudding user response:
I don't know what do you want by that but You can return that class when you run that function.
For example:
def main():
class Aclass():
def __init__(self):
pass
return Aclass
f = main()
y = f()
Your class now is y variable
Your way in writing code is not correct but every one has thoughts or ideas that they want to do it.