Hi Gavin,
You have Reply To set to your personal email address instead of allowing
replies to go to the list for everyone to contribute to the discussion.
Was that intentional?
On Fri, May 01, 2020 at 04:53:31PM -0000, gbs--- via Python-ideas wrote:
> In cases where it makes sense to do explicit type checking (with ABCs
> or whatever), I really detest the look of the isinstance() function.
Is it just the `isinstance` function you detest, or all functions?
If it's just `isinstance`, then you can alias it:
is_a = isinstance
if is_a(thing, Fruit): # yuck this is worse
but if it is all functions, then I think you have no choice but to
either live with it or shift languages, because the syntax for functions
is too deeply baked into Python to change now.
[...]
> What I really want to write is:
>
> if thing is a Fruit and thing is not an Apple:
>
> and after thinking about it on and off for a while I wonder if it
> might indeed be possible to teach the parser to handle that in a way
> that eliminates almost all possible ambiguity with the regular "is",
> including perhaps 100% of all existing standard library code and
> almost all user code?
Whether it is possible is only half the question. Whether it is
desirable is the other half.
My first language was Apple's Hypertalk back in the 1990s, which used a
very English-like syntax where things like "is a" would have been right
at home. For example:
if there is a file "myfile.pct" then ...
Functions could be written in either traditional parenthesized format,
or English-like prefix operator syntax:
root = sqrt(value)
put the sqrt of value into root
It's been two decades since I've last written a line of Hypertalk code
and I still miss it :-) so I'm definitely sympathetic to your request.
But I fear that few others will be.
If we wanted this new operator, I think it should combine
isinstance and issubclass:
obj is a T
should be equivalent to:
issubclass(obj, T) if isinstance(obj, type) else isinstance(obj, T)
and T is a type or tuple of types.
The rules in English for when to use "a" versus "an" depend
on the *sound* of the following word, not the initial letter. Blindly
applying the rule "an before vowels otherwise a" is wrong:
an uncle # correct
an unicorn # wrong
so I propose that the interpreter allow either, and we leave it to the
programmer and/or linters to enforce good English grammar:
obj is an int
obj is an float # accepted by interpreter
obj is a int # also accepted by interpreter
In other words, the "is-a" operators would become:
is a[n]
is not a[n]
The above is the easy part. The hard part is justifying why this should
become an operator.
> Maybe this has been considered at some point in the past? The "is
> [not] a|an" proposal would at least be a strong contender for "hardest
> thing to search for on the internet" lol.
Indeed :-)
But for what it's worth, I don't remember coming across this myself.
--
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/WPMJBKBNR3PQFFDXWUYJ32OUJRTMBXWP/
Code of Conduct: http://python.org/psf/codeofconduct/