[Python-Dev] PEP 495 Was: PEP 498: Literal String Interpolation is ready for pronouncement

Terry Reedy tjreedy at udel.edu
Sat Sep 12 07:20:24 CEST 2015


On 9/11/2015 8:40 PM, Alexander Belopolsky wrote:

> The insanity I am dealing with now is specific to Python datetime which
> wisely blocks any binary operation that involves naive and aware
> datetimes, but allows comparisons and subtractions of datetimes with
> different timezones.  This is not an unusual situation given a limited
> supply of binary operators, Python has to reuse them in less than ideal
> ways. For example,
>
>  >>> a = 'a'
>  >>> b = 'b'
>  >>> c = 5
>  >>> (a + b) * c == a * c + b * c
> False
>
> is less than ideal if you hold the distributive law of arithmetic
> sacred.

In mathematics, algebra is, put simply, the study of binary operations. 
None of the laws for particular operations on particular sets is 
'sacred'.  They are simply facts. Or possibly axioms whose consequences 
are to be explored.  A mathematician has no problem with 'a'+'b' != 
'b'+'a'.  After closure, associativity is the most 'basic' operation, 
but non-associative operations are studied.

The equality relation, mapping pairs of members of a set to True or 
False is a different matter.  Being an equivalence relation is 
fundamental to both normal logic, algebraic proofs, and the definition 
of sets.  It is also required for the 'proper' operation of Python's 
sets. (Lets leave nans out of the discussion).

>  Similarly, '-' is reused in datetime for two different
> operation: if s and t are datetimes with the same tzinfo, s - t tells
> you how far to move hands on the local clock to arrive at s when you
> start at t.  This is clearly a very useful operation that forms the
> basis of everything else that we do in datetime.  Note that for this
> operation, it does not matter what kind of time your clock is showing or
> whether it is running at all.  We are not asking how long one needs to
> wait to see s after t was shown.  We are just asking how far to turn the
> knob on the back of the clock.  This operation does not make sense when
> s and t have different tzinfos, so in this case a '-' is reused for a
> different operation.  This operation is much more involved.  We require
> an existence of some universal time (UTC) and a rule to convert s and t
> to that time and define s - t as s.astimezone(UTC) - t.timezone(UTC).
> In the later expression '-' is taken in the "turns of the knob" sense
> because the operands are in the same timezone (UTC).

Datetime members, are rather unusual beasts. They are triples consisting 
of a member of a discrete sequence (with some odd gaps), a tz tag, and a 
0/1 fold tag. The tz tags divide datetimes into equivalence classes. 
The '-' operation is also unusual in being defined differently for pairs 
in the same or different equivalence classes.

> Clearly, when we "abuse" the mathematical notation in this way,

Mathematicians wildly overload operater/relation symbols. '=' is the 
only one I can think of with more-or-less universal properties.

> we cannot expect mathematical identities

which are context dependent

> to hold

in a different context.  Right.

> and it is easy to find for
> example, aware datetimes u, t, and s such that (t - u) - (s - u) ≠ t - s.

Datetime '-' (subtraction) should be documented as an unusual overloaded 
use which does not have the normal properties that the naive might expect.

Within the constraint on '=', there are two choices for designing an 
operation.
1. Define the operation the way you want, and live with the consequent 
properties (or absence of properties).
2. Decide the properties you require, and live with the consequent 
restriction on the definition.

-- 
Terry Jan Reedy




More information about the Python-Dev mailing list