[Edu-sig] Technical question

Seth David Schoen schoen@loyalty.org
Thu, 10 May 2001 17:55:37 -0700


Kirby Urner writes:

> I have a pyfraction.py and a polynomial.py, which contain
> Fraction and Poly classes respectively.
> 
> My problem is each class needs to know about the other, 
> meaning I'm now doing an isinstance(other,Fraction) in 
> Poly, and an isinstance(other,Poly) in Fraction.
> 
> So how do I handle this.  I have an 
> 
>      import Fraction from pyfraction 
> 
> at the top of polynomial.py, and an 
> 
>      import Poly from polynomial
> 
> at the top of pyfraction.py  
> 
> This is obviously NOT the way to handle it, because
> it's a loop.  In trying to import Fraction, it hits the
> line to import polynomial, which it's still trying to 
> deal with.
> 
> This problem arises now that I'm using isinstance(foo,bar)
> and need bar as a global.  Earlier, I was doing 
> type(foo).__class__.__name__ == "bar", which was ugly,
> but didn't require that I actually have any bars hangin'
> around.

I didn't have any trouble with a mutual import under Python 1.5.2:


sample.py:

#!/usr/bin/python

import woo

class Ample:
	def __init__(self):
		self.woo = woo.Woohoo()



woo.py:

#!/usr/bin/python

import sample

class Woohoo:
	def __init__(self):
		pass

	def gimmeanample(self):
		return sample.Ample()

-- 
Seth David Schoen <schoen@loyalty.org>  | And do not say, I will study when I
Temp.  http://www.loyalty.org/~schoen/  | have leisure; for perhaps you will
down:  http://www.loyalty.org/   (CAF)  | not have leisure.  -- Pirke Avot 2:5