Classes question

Emmanuel Jeandel ejeandel at ens-lyon.fr
Thu Oct 31 23:53:42 EST 2002


Il est arrivé, un jour, à Aahz d'écrire :
> In article <20021031205456.GA8006 at ens-lyon.fr>,
> Emmanuel Jeandel  <E_Jeandel at mail.dotcom.fr> wrote:
> >
> >I have the following code : 
> >
> >class A:
> >	  def __init__(self):
> >	  	  pass
> >
> >	  def f(self):
> >	  	  self.x = B()
> >
> >class B(A):
> >	  def __init__(self):
> >	  	  A.__init__(self)
> >
> >
> >I would like to have the class A and the class B in two differents files.
> >Is it possible ? 
> >I think that it is not directly possible, but if there is a mean with a
> >small change in the code...
> 
> Assuming you put the code for A in classA.py, you can make class B work
> by adding the line
> 
> from classA import A
> 
> before the "class B" statement.
Obviously no. Please note that A needs to know about B in this example
(line: self.x = B() in the class A).

If i keep the same examples, and copy them in files classA and classB then
  i. Add "from classA import A" in classB.py
 ii. Create C.py with : 
          import classA
		  import classB
 		  a = classA.A()
		  b = classB.B()
		  a.f()

I get an undefined in a.f()   ( even if i replace B() par classB.B() )

Of course, if i do "from classB import B" in classA.py, i have a cyclic
dependence, and this not works (cannon import name A).

I don't know is there is a solution, i'm just asking..

Emmanuel



More information about the Python-list mailing list