Can you please explain why a multi-part second-argument must be a tuple
and not any other form of collection-type?
Can you please explain why a multi-part second-argument must be a
tuple and not any other form of collection-type?
The signature is: isinstance(object, classinfo)
leading to "classinfo" of:
1/ a single class/type, eg int
2/ a tuple of same, eg ( int, str, )
3/ a union type, eg int | str (v3.10+)
Can you please explain why a multi-part second-argument must be a tuple
and not any other form of collection-type?
On 2023-08-02, dn <PythonList@DancesWithMice.info> wrote:
Can you please explain why a multi-part second-argument must be a tuple
and not any other form of collection-type?
The following comment may hold a clue:
if (PyTuple_Check(cls)) {
/* Not a general sequence -- that opens up the road to
recursion and stack overflow. */
https://github.com/python/cpython/blob/main/Objects/abstract.c#L2684
Plus an almost total lack of demand for change I should think.
(<class 'float'>, <class 'complex'>)target_object = ...
inner_tuple = float, complex
inner_tuple
Falseisinstance( target_object, ( str, inner_tuple, int, ), )
On Sat, 5 Aug 2023 at 09:08, dn via Python-list <python-list@python.org> wrote:Yes. Thanks Chris!
On 03/08/2023 11.38, Jon Ribbens via Python-list wrote:
On 2023-08-02, dn <PythonList@DancesWithMice.info> wrote:
Can you please explain why a multi-part second-argument must be a tuple >>>> and not any other form of collection-type?
The following comment may hold a clue:
if (PyTuple_Check(cls)) {
/* Not a general sequence -- that opens up the road to
recursion and stack overflow. */
https://github.com/python/cpython/blob/main/Objects/abstract.c#L2684
Plus an almost total lack of demand for change I should think.
Thanks for the reference!
Am not proposing a change (have learned 'the rule' and accepted
life-as-it-is), but was curious about the restriction: why not any
(reasonable sequence)?
There are quite a few places where the only option is a tuple.
Traceback (most recent call last):"spam".startswith(["sp", "h"])
File "<stdin>", line 1, in <module>
TypeError: startswith first arg must be str or a tuple of str, not list
... except [ValueError, IndexError]: passtry: 1/0
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: catching classes that do not inherit from BaseException is
not allowed
It simplifies a lot of checks. Either it's a tuple or it's a single
thing. A *lot* of values are iterable, but only tuples are tuples.
As the C comment notes, this also means you don't have to worry about recursion; otherwise, even core language features like exception
handling would have to iterate over a thing while ensuring that they
don't get stuck in self-referential objects. (Incidentally, it seems
that exception handling doesn't bother with recursion *at all*, and
won't catch "(a, (b, c))" - but even if recursion is handled, it can't
go infinite, short of some serious messing around in ctypes or the C
API.)
Faced with a situation where an argument may be a scalar-value or an iterable, I'll presume the latter, eg throw it straight into a for-loop.
If that fails (because the argument is a scalar), use try-except to
re-route the logic.
On 03/08/2023 11.38, Jon Ribbens via Python-list wrote:
On 2023-08-02, dn <PythonList@DancesWithMice.info> wrote:
Can you please explain why a multi-part second-argument must be a tuple
and not any other form of collection-type?
The following comment may hold a clue:
if (PyTuple_Check(cls)) {
/* Not a general sequence -- that opens up the road to
recursion and stack overflow. */
https://github.com/python/cpython/blob/main/Objects/abstract.c#L2684
Plus an almost total lack of demand for change I should think.
Thanks for the reference!
Am not proposing a change (have learned 'the rule' and accepted life-as-it-is), but was curious about the restriction: why not any (reasonable sequence)?
Traceback (most recent call last):"spam".startswith(["sp", "h"])
... except [ValueError, IndexError]: passtry: 1/0
On Sat, 5 Aug 2023 at 09:36, dn via Python-list <python-list@python.org> wrote:
Faced with a situation where an argument may be a scalar-value or an
iterable, I'll presume the latter, eg throw it straight into a for-loop.
If that fails (because the argument is a scalar), use try-except to
re-route the logic.
That's great as long as you aren't expecting to handle strings.
The string "spam" is sometimes equivalent to the list ["s", "p",
"a", "m"] and sometimes not.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 03:53:08 |
Calls: | 10,387 |
Calls today: | 2 |
Files: | 14,061 |
Messages: | 6,416,778 |