我无法重现您的错误。
作为试验我已经包括类似的方法来既正方形和三角形模块,分别打印方形或三角形,类似的东西:
def a():
print('square')
我把它称为在__init__
形状类的和收到预期输出。
class shape:
def __init__(self, shape_id):
if shape_id == '001':
from shapes import triangle as imported_shape
else:
from shapes import square as imported_shape
imported_shape.a()
但是如果你想使用进口模块别处地方的__init__
- 你应该assing imported_shape
到自我:
class shape:
def __init__(self, shape_id):
if shape_id == '001':
from shapes import triangle as imported_shape
else:
from shapes import square as imported_shape
self.imported_shape = imported_shape
在这之后 - 你可以在其他方法来访问你的模块形状类:
def test(self):
self.imported_shape.a()
阿科录制您的需要和Python代码standarts - 这将是更好地导入您的模块的顶部形状和__init__
做这样的事情:
import shapes
class shape:
def __init__(self, shape_id):
if shape_id == '001':
self.imported_shape = shapes.triangle
else:
self.imported_shape = shapes.square
OOP例如:
Asuming是正方形和三角形有same-命名的类:
from shapes.square import square
from shapes.triangle import triangle
class shape(square, triangle):
def __init__(self, shape_id):
if shape_id == '001':
super(triangle, self).__init__()
else:
super(square, self).__init__()
您的项目结构是什么? –
进口留在任何对象的功能范围 – PRMoureu
@YaroslavSurzhikov更新项目结构的问题 – MrPyCharm