I am working on a factory in Python and would like to make all the imports "private".
So something like this:
from my_classes import MyClass1, MyClass2
class MyFactory(object):
def __init__(self, class_name):
if class_name == "my_class_1":
return MyClass1()
elif class_name == "my_class_2":
return MyClass2()
In this case I would like to have the caller be able to use MyFactory, and not see MyClass1 and MyClass2.
I know there is no such thing as private in Python.
What I tried is from my_classes import MyClass1 as _my_class_1
but then I get a message Constant variable imported as non-constant
For now I have solved it like this:
class MyFactory(object):
def __init__(self):
do my init stuff here
def create_myclass_1(self):
from my_classes import MyClass1
return MyClass1()
def create_myclass_2(self):
from my_classes import MyClass2
return MyClass2()
This does not look very Pythonic to me, but I might be wrong.
Could someone tell me if there is a better way to do this?