How much sanity checking is required for function inputs?

Steven D'Aprano steve at pearwood.info
Sun Apr 24 14:00:07 EDT 2016


On Sun, 24 Apr 2016 04:40 pm, Michael Selik wrote:


> I think we're giving mixed messages because we're conflating "constants"
> and globals that are expected to change.

When you talk about "state", that usually means "the current state of the
program", not constants. math.pi is not "state".


> In our case here, I think two clients in the same process sharing state
> might be a feature rather than a bug. Or at least it has the same behavior
> as the current implementation.

I don't think so. Two clients sharing state is exactly what makes thread
programming with shared state so exciting.

Suppose you import the decimal module, and set the global context:

py> import decimal
py> decimal.setcontext(decimal.ExtendedContext)
py> decimal.getcontext().prec = 18
py> decimal.Decimal(1)/3
Decimal('0.333333333333333333')

Great. Now a millisecond later you do the same calculation:

py> decimal.Decimal(1)/3
Decimal('0.33333')


WTF just happened here??? The answer is, another client of the module, one
you may not even know about, has set the global context:

decimal.getcontext().prec = 5

and screwed you over but good.




-- 
Steven




More information about the Python-list mailing list