I have three modules in the same folder.
The first module, run.py, is the main program.
The second module, called shapes.py, contains a class called "Shape"
The third module, called circles.py, that contains a class called "Circle" which inherits from Shape.
The code is written as follows
run.py
from shapes import Shape from circles import Circle a = Circle() a.print_test()
shapes.py
class Shape(object):
def print_name(self):
print "I am a generic shape"
circles.py
class Circle(Shape):
def print_name(self):
print "I am a circle"
I want to be able to run the program and have the console say "I am a circle", but it throws an exception when importing circles saying that "Shape is not defined".
I can always tell circles.py to import the Shape class, but that's not what I want. What if they're not in the same folder? What if there's a complicated hierarchy of folders?
It feels like I'm importing the shapes module twice that way unnecessarily just so that I can import circles.
What can I do? (well, in this case, run.py probably doesn't even need to import Shape, but if I had some other modules "triangles", "hexagons", and "pentagons" I don't want them all to have to import Shape)
EDIT: I could also just put them all in the same module cause they're shapes! But this sort of problem might arise some time.
Circleshould be aShapethen it must know what aShapeis -- otherwise, how is it supposed to inherit from it?!run.pyknowing what aShapeis doesn't mean thatcircles.pyknows what aShapeis. That's just how Python works.