problems with circular references

jimh jim at jim.jim
Thu Mar 28 12:44:27 EST 2002


> 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.

Regardless of using exceptions or not, each class still has a list of errors
that can occur, and those errors need to be mapped to (possibly localized)
strings.

> 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.

You lose modularity when you do this.  The way it is now, each class is
self-contained - everything about that class is in a single file.  It is
much easier to share classes with other developers (which we do) this way.
Having said that, this may be preferable over having an additional 50 ".h"
files.  I was just hoping there would be a good "Pythonic" way to deal with
this situation.

>
> 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,
>





More information about the Python-list mailing list