problems with circular references

Hans Nowak wurmy at earthlink.net
Wed Mar 27 23:12:43 EST 2002


jimh wrote:
> 
> I am working on python code that consist of quite a few files (about 50;
> each file contains one class).  All errors returned by functions are numeric
> and are defined in the individual files.  There is a MessageCatalog.py class
> which will map these numbers into strings.  This MessageCatalog.py has to
> import all of the 50 other files to have access to their error numbers.  So
> far, so good - no problems yet.
> 
> Here is the problem: Most of these 50 classes need to use the MessageCatalog
> themselves.  This means, of course, that classA imports MessageCatalog which
> imports classA.  This doesn't work very well.
> 
> Does anyone have ideas on how to deal with this situation?

Like some others already pointed out, exceptions are probably
a better and more Pythonic way to handle this. But maybe, for
some reason, you cannot use them. I cannot see your code,
so I can't tell.

How about putting all error codes in one file? E.g. errors.py:

  FOO_ERROR = 1000
  BAR_ERROR = 1001
  BAZ_ERROR = 1002
  ...etc...

Now all your 50 files can import errors.py and use the error
codes, rather than having to define them themselves.

All your MessageCatalog class needs to do is import that
errors.py file as well. Since it seems to be used for
mapping, have you considered using a dictionary? E.g.

  error_mappings = {
    FOO_ERROR: "Foo not implemented",
    BAR_ERROR: "Ixnay on the bar",
    ...etc...
  }

Again, I can't see your code, so maybe I'm oversimplifying.
Still, knowing your description only, this is how I
would set it up.

HTH,

-- 
Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA==')) 
# decode for email address ;-)
The Pythonic Quarter:: http://www.awaretek.com/nowak/



More information about the Python-list mailing list