0

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.

5
  • 3
    "I can always tell circles.py to import the Shape class, but that's not what I want." Then Python isn't the language for you. Commented Jul 7, 2011 at 20:07
  • My design would eventually have "triangles" and "hexagons" and a number of other shapes, where they would all inherit Shape. But then I might move shapes.py to another folder, meaning I would have to go and change all of the import statements. I would like to avoid having to make such changes. Commented Jul 7, 2011 at 20:18
  • 1
    @Keikoku: if Circle should be a Shape then it must know what a Shape is -- otherwise, how is it supposed to inherit from it?! Commented Jul 7, 2011 at 20:20
  • @katrielalex, the idea is to have run.py import it so that the circles doesn't have to import it, because I could have triangles and hexagons, but that approach didn't work so maybe there's another way. Commented Jul 7, 2011 at 20:22
  • @Keikoku: but run.py knowing what a Shape is doesn't mean that circles.py knows what a Shape is. That's just how Python works. Commented Jul 7, 2011 at 20:33

3 Answers 3

3

Import shapes from within circles.py:

from shapes import Shape

class Circle(Shape):
   ...

In Python, each module must import whatever it needs. It can't rely on any other module to do the importing for it.

Sign up to request clarification or add additional context in comments.

2 Comments

@katrielalex: I had them backwards at first. I think it's fixed now.
@Greg: they were still backwards, fixed it for ya.
3

You need to import all classes you are using in a python module. In your first module (run.py) you are not using Shape, only Circle, so you can omit it there. run.py does not need to know how Circles are defined at all.

In circles.py however, you do need to import shapes, as you are basing your Circle class on the Shape class, so it needs to have access to the definition there.

from shapes import Shape

class Circle(Shape):
    def print_name(self):
         print "I am a circle"

I'd recommend you use a tool like pyflakes to check your files for errors like these. You can hook such a script up with various editors to be run automatically, giving you instant feedback whenever you save your python files.

2 Comments

@Martijn Pieters, so basically even though I imported Shape in run.py, circle.py doesn't know about Shape?
Exactly. run.py is a separate module from circle.py. Each module only knows about the names it defines or imports. There is nothing magical about circle.py that would enable it to know what run.py has imported.
0

I think you have to initialize the shapes superclass in the circle class. You can do this via constructors. So in your circle.py class you would need to have something like the following:

class Circle(Shapes):
    def __init__(self):
        Shapes.__init__(self)
        # put the rest of your circle code here.

Magnus Lie Hetland's book, "Beginning Python: From Novice to Professional" covers this area fairly well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.