On Sun, Apr 19, 2020 at 11:58:01AM -0400, David Mertz wrote:
> Understand that I'm not really advocating for a magic function like Q(). I
> think it's important to keep in mind that anyone COULD create such a thing
> for 20 years, and very few people bothered to.
You don't really know how many people are using such a thing in their
private code. Its also a pretty obscure thing to use: frame hacks are
not something that most Python programmers know anything about.
The fact that your Q() and similar solutions are described as "magic" or
"sorcery" demenstrates that even the creators of the functions are aware
that they are somewhat unclean hacks that shouldn't be relied on.
At least with a language feature, the hack is official, and it's the
responsibility of the language devs to keep it working, at which point
it ceases to be a hack and becomes just part of the language.
With a Python level hack, if something breaks, it's *my* responsibility
to fix it, and I'll get zero sympathy from anyone else.
So if the only choices are "deal with the middling pain of `name=name`
function calls" versus "take on an unknown amount of technical debt by
using an unsupported hack", I'll keep writing `name=name` thank you very
much :-)
> If repeating names was
> actually such a big pain point, a decent solution has been available for a
> very long time, but it didn't itch enough for many people to write a few
> lines with some slight magic to scratch it.
The fact that it requires the use of a private function means that many
people will not consider it a decent solution, but technical debt which
will have to be paid eventually.
When I compare the Python community to (say) the Java community, I am
pleasantly surprised about how seriously most Python people are about
not using _private APIs. Sometimes I feel that Java programmers spend
half their day trying to protect their code from other people using it,
and the other half trying to break that protection on other people's
code. Whereas the Python community, generally, wouldn't touch someone
else's _private API if you paid them.
There's another issue. Put yourself in the shoes of someone who *isn't*
the author of your Q() function, and you come across this function call:
func(arg, **Q(alpha))
It looks like a regular function. Obviously it must return a dict,
because it is passed to the dict-unpacking operator. You don't know that
there's anything special about Q, it just looks like a plain old Python
function, so you know that Q takes the *value* of `alpha`, because
that's how Python function works.
Would you recognise this as a different spelling of `alpha=alpha`?
I doubt it. And if you did, you would probably say "Wait a minute, how
the hell does Q know the *name* from the *value* of the variable?
Most objects don't have names!"
If you *didn't* recognise Q as magic, you might be tempted to change it.
Say you need a couple of extra arguments:
func(arg, **Q(alpha, 'something', beta+1))
and who the hell knows what that will do! (Cue the Sorceror's Apprentice
music.) It might even work by accident, and that would be the worst
thing of all, because then it could stop working when we least expect
it.
Sorry David, when I see your magic Q() I see technical debt, not a
viable solution for production code.
--
Steven
_______________________________________________
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/JRUONSLVDJGVOTKFRFXBBAHVAFSIZ3ND/
Code of Conduct: http://python.org/psf/codeofconduct/