On Wed, Sep 2, 2020 at 3:48 AM Stephen J. Turnbull
<[email protected]> wrote:
>
> A digression on abuse of operators, notation, and "abuse of notation".
>
> Steven D'Aprano writes:
> > On Sun, Aug 30, 2020 at 05:49:50PM +1200, Greg Ewing wrote:
>
> > > I don't see why we need to pick one use case to bless as the
> > > official "true" use case.
> >
> > We do that for every other operator and operator-like function.
> >
> > Operator overloading is a thing, so if you want `a + b` to mean
> > looking up a database for `a` and writing it to file `b`, you
> > can. But the blessed use-cases for the `+` operator are numeric
> > addition and sequence concatenation.
>
> Of course the example is abusive. But not being "blessed" isn't the
> reason. (And shouldn't the notation be "b += a" for that operation? ;-)
>
Possibly a better example would be path division in Python, or stream
left/right shift in C++. What does it mean to divide a path by a
string? What does it mean to left-shift std::cout by 5? Those uses
don't make a lot of sense based on the operations involved, but people
accept them because the symbols look right:
>>> pathlib.Path("/") / "foo" / "bar" / "quux"
PosixPath('/foo/bar/quux')
Is that abuse of notation or something else? Whatever it is, it's not
"operator overloading" in its normal sense; division is normally the
inverse of multiplication, but there's no way you can multiply that by
"quux" to undo that last operation, and nobody would expect so. Maybe
we need a different term for this kind of overloading, where we're not
even TRYING to follow the normal semantics for that operation, but are
just doing something because it "feels right".
ChrisA
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/ZAV7YEBWDL3KBXM2VI6QYV66H4AIV5JW/
Code of Conduct: http://python.org/psf/codeofconduct/