Я просматривал простой фабричный пример
from __future__ import generators
import random
class Shape(object):
# Create based on class name:
def factory(type):
#return eval(type + "()")
if type == "Circle": return Circle()
if type == "Square": return Square()
assert 0, "Bad shape creation: " + type
factory = staticmethod(factory)
class Circle(Shape):
def draw(self): print("Circle.draw")
def erase(self): print("Circle.erase")
class Square(Shape):
def draw(self): print("Square.draw")
def erase(self): print("Square.erase")
# Generate shape name strings:
def shapeNameGen(n):
types = Shape.__subclasses__()
for i in range(n):
yield random.choice(types).__name__
shapes = \
[ Shape.factory(i) for i in shapeNameGen(7)]
for shape in shapes:
shape.draw()
shape.erase()
и пытался разбить его на отдельные файлы.
main.py
from __future__ import generators
import random
from factory.shapes.circle import Circle
from factory.shapes.sqaure import Square
class Shape(object):
# Create based on class name:
def factory(type):
#return eval(type + "()")
if type == "Circle": return Circle()
if type == "Square": return Square()
assert 0, "Bad shape creation: " + type
factory = staticmethod(factory)
# Generate shape name strings:
def shapeNameGen(n):
types = Shape.__subclasses__()
for i in range(n):
yield random.choice(types).__name__
shapes = \
[ Shape.factory(i) for i in shapeNameGen(7)]
for shape in shapes:
shape.draw()
shape.erase()
circle.py
from __future__ import generators
import random
from factory.main import Shape
class Circle(Shape):
def draw(self): print("Circle.draw")
def erase(self): print("Circle.erase")
square.py
from __future__ import generators
import random
from factory.main import Shape
class Square(Shape):
def draw(self): print("Square.draw")
def erase(self): print("Square.erase")
и при запуске я получаю ImportError: невозможно импортировать имя 'Circle'
Так что, хотя пример работает, когдавсе классы находятся в одном модуле, похоже, возникают проблемы при импорте их из отдельных модулей.Есть идеи?