-2

Possible Duplicate:
Python mutually dependent classes (circular dependencies)

I have 2 class in the same module/file, and they use one another.

class ClassA:
    x = ClassB()

class ClassB:
    x = ClassA()

ClassA would have a problem since the declaration of ClassB is after ClassA, hence ClassB is not found.

How do I solve this? Can I somehow import ClassB?


Update: My bad. The example above has a circular reference, and that disgusted many people.. Let me try again:

class ClassA:
    x = 1
    y = ClassB.x

class ClassB:
    x = 2
    y = ClassA.x

Can I somehow import ClassB before ClassA implementation? Or some sort of forward class declaration.

Community
  • 1
  • 1
samwize
  • 25,675
  • 15
  • 141
  • 186
  • No they don't. It's to illustrate the class using one another in some way. – samwize Aug 13 '12 at 16:51
  • If these classes were in separate files and you tried to import a class from one file from the other file you would have a *circular import* (there are plenty of questions on this on this site). Regardless of the language this is to be avoided and is generally a sign of a bad desgin. Perhaps you can post the actual problem you are trying to solve and we can suggest alternative implementations. – Chris Aug 13 '12 at 17:00
  • As a matter of fact I don't have a circular reference. This problem just crossed my mind. – samwize Aug 13 '12 at 17:03
  • 1
    this seems like something to solve with inheritance. It's hard to say without actual code – Ryan Haining Aug 13 '12 at 17:18
  • Even after your edit you do have a circular reference. – Chris Aug 13 '12 at 17:36
  • Are you actually trying to create class attributes as shown or do you really want instance attributes? – martineau Aug 13 '12 at 21:45
  • @martineau Class attributes. Does it matter? – samwize Aug 14 '12 at 03:31

1 Answers1

5

This works:

class ClassA:
    pass

class ClassB:
    x = ClassA()

ClassA.x = ClassB()

But is it really what you want to do? The need to do this makes me think that you should probably rethink your design...

Of course, if they don't need to be class attributes (e.g. if instance attributes work OK because not every instance of ClassX needs to share the same data), you can do this:

class ClassA(object): #inheriting from object is always a good idea
    def __init__(self):
        self.x = ClassB()

class ClassB(object):
    def __init__(self):
        self.x = ClassA() 

This works because the object creation (and therefore lookup in the globals dictionary) doesn't happen until the classes are instantiated.

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • That's not what I want.. My first attempt was to `import ClassB`, but that won't work. I would want something along that line. – samwize Aug 13 '12 at 16:55
  • 3
    @samwize -- I'm sorry, but if this isn't what you want, I'm not understanding your question ... `ClassB` has an attribute `x` which is an instance of `ClassA` and `ClassA` has an attribute `x` which is an instance of `ClassB`. How is that not what you were attempting in your snippet? – mgilson Aug 13 '12 at 16:58