#define equivalents and config files

Peter Hansen peter at engcorp.com
Thu Mar 21 18:59:24 EST 2002


Jeff Shannon wrote:
> 
> Dave Swegen wrote:
> 
> > > Rich Salz wrote:
> > > >
> > > > > Is there a pythonic way of doing something similiar to Cs #define?
> > > >  >... are globals the only way to go?
> > > >
> > > > You can at least make them class attributes
> > > >         class constants:
> > > >                 pi = 3.14159
> > > >                 e = 2.71828
> > > > adding your own setattr can serve to make them read-only.

> You can use some interesting hacks to make the constants module read-only,
> if necessary (see the ASPN Python Cookbook), but since I'm typically the
> only person using my code I've decided to simply trust myself.  ;)

And thank you for that, since if, after all, you decide to distribute
your code, the rest of us can improve it without changing your source, 
which wouldn't be possible if you'd gone all whips and chains on us 
and made it read-only:

import constants                  # get Jeff's constants
constants.pi = 3.141592653589793  # tighten up pi a little...

:-)

-Peter



More information about the Python-list mailing list