Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Many alligators will be slain, but the swamp will remain.


devel / comp.lang.python / isinstance()

SubjectAuthor
* isinstance()dn
+* Re: isinstance()Jon Ribbens
|+- Re: isinstance()dn
|+- Re: isinstance()Chris Angelico
|+- Re: isinstance()dn
|+- Re: isinstance()Chris Angelico
|`- Re: isinstance()Grant Edwards
`- Re: isinstance()Stefan Ram

1
isinstance()

<mailman.200.1691014515.23016.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23838&group=comp.lang.python#23838

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: PythonL...@DancesWithMice.info (dn)
Newsgroups: comp.lang.python
Subject: isinstance()
Date: Thu, 3 Aug 2023 10:14:56 +1200
Organization: DWM
Lines: 38
Message-ID: <mailman.200.1691014515.23016.python-list@python.org>
References: <f7bd1016-ad65-2c8c-b5b2-2750632c9366@DancesWithMice.info>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de Uu0pxb5U+NL4AmILIodMhA4ap37aY99rkcDIn7c0tUBw==
Cancel-Lock: sha1:JItJ3HNqwngSJIzFRmIcIiSYNes= sha256:BdOHV4u6QtW2F+EKRHkN2tPWztg8LrnPfOSg1z29VQA=
Return-Path: <PythonList@DancesWithMice.info>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=danceswithmice.info header.i=@danceswithmice.info
header.b=kV+gV/Wn; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.016
X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'argument': 0.04;
'underlying': 0.07; '=dn': 0.09; 'from:addr:danceswithmice.info':
0.09; 'from:addr:pythonlist': 0.09; 'int': 0.09; 'url:reference':
0.09; 'arguments': 0.16; 'correcting': 0.16; 'message-
id:@DancesWithMice.info': 0.16; 'received:cloud': 0.16;
'received:rangi.cloud': 0.16; 'tuple': 0.16; 'python': 0.16;
'to:addr:python-list': 0.20; "i've": 0.22; 'maybe': 0.22;
'discussion': 0.25; "isn't": 0.27; 'series': 0.28; 'asked': 0.29;
'header:User-Agent:1': 0.30; 'seem': 0.31;
'header:Organization:1': 0.31; 'question': 0.32; 'same,': 0.32;
'words,': 0.32; 'received:192.168.1': 0.32; 'but': 0.32; 'there':
0.33; 'yes,': 0.35; 'using': 0.37; 'received:192.168': 0.37;
'single': 0.39; 'list': 0.39; 'still': 0.40; 'appears': 0.40;
'explain': 0.40; 'forms': 0.40; 'want': 0.40; 'leading': 0.63;
'accept': 0.67; 'signature': 0.76; 'out,': 0.78; 'enquiring':
0.84; 'minds': 0.84; 'type,': 0.84; 'of:': 0.91; 'union': 0.91
DKIM-Filter: OpenDKIM Filter v2.11.0 vps.rangi.cloud 6375B3396
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=danceswithmice.info;
s=staff; t=1691014507;
bh=yCPsfBWLgWoCIhneoQDolsFPHb/cymem0s5iX/Y1cQ0=;
h=Date:To:From:Subject:From;
b=kV+gV/Wnh1e354/qrRLmhRlakn3UsXrMK05KrxzHjHJTKu2Wwx4hzBGYWbd5DkZPc
W7x+e/bcyqzJTcmwuHbSC0u3T8BGuKYjFhLB/BjDOY99A9dVgPQGJi8qGvOtaSwEMK
o1z72OjVE16z/JBjDx2qPh8nuEsaf9rBtsDTPN2zUDCuQ1th5N4OH5/eOKD+0+IhIX
sJSRUpq/T7u6M+X2XX3xBra7tHi3xBYeOKGahJFA65TX0a7iLDlmxUJrUDzkB5elxq
0BKcM26R9xATDDQjWeBi+FE+d9XXcs0OhPCyQ80QVyChfOPkwAM4bR+h/hC64TkesC
WaIK85swP9PmA==
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.13.0
Content-Language: en-GB
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <f7bd1016-ad65-2c8c-b5b2-2750632c9366@DancesWithMice.info>
 by: dn - Wed, 2 Aug 2023 22:14 UTC

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+)

A question was asked about this at last night's PUG-meeting. The person
was using the union option, but from Python v3.8. Sorting that out, he
replaced the union with a list. No-go! Before correcting to a tuple...

Why does the second argument need to be a tuple, given that any series
of non-keyword arguments is a tuple; and that such a tuple will still
need to be delimited by parentheses. In other words, other forms of
delimiter/collection, eg list and set; being a series of
elements/members would seem no different.

Yes, the underlying C operation appears to only accept a single argument
(am not C-soned, mea culpa!).

There is some discussion about hashability, but isn't it coincidental
rather than constructive? (again, maybe I've not understood...)

Enquiring minds want to know...

Web.Refs:
https://docs.python.org/3/library/functions.html?highlight=isinstance#isinstance
https://docs.python.org/3/c-api/object.html?highlight=isinstance#c.PyObject_IsInstance
https://docs.python.org/3/reference/datamodel.html?highlight=isinstance

--
Regards,
=dn

Re: isinstance()

<slrnuclq72.351a.jon+usenet@raven.unequivocal.eu>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23839&group=comp.lang.python#23839

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: jon+use...@unequivocal.eu (Jon Ribbens)
Newsgroups: comp.lang.python
Subject: Re: isinstance()
Date: Wed, 2 Aug 2023 23:38:10 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <slrnuclq72.351a.jon+usenet@raven.unequivocal.eu>
References: <f7bd1016-ad65-2c8c-b5b2-2750632c9366@DancesWithMice.info>
<mailman.200.1691014515.23016.python-list@python.org>
Injection-Date: Wed, 2 Aug 2023 23:38:10 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="2ae3aa3387e66e8308f4f2d0a21666c7";
logging-data="349441"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/yjApoO7MbVLndPti0tdkFULmdzxFDUnw="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:dzkCvCSrrNFJNORXLtYhBtAkXVI=
 by: Jon Ribbens - Wed, 2 Aug 2023 23:38 UTC

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.

Re: isinstance()

<isinstance-20230803141223@ram.dialup.fu-berlin.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23841&group=comp.lang.python#23841

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: isinstance()
Date: 3 Aug 2023 13:12:59 GMT
Organization: Stefan Ram
Lines: 10
Expires: 1 Jul 2024 11:59:58 GMT
Message-ID: <isinstance-20230803141223@ram.dialup.fu-berlin.de>
References: <mailman.200.1691014515.23016.python-list@python.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de sfw7DsdFXur4uECi3iUrQwVf2GFXbM70yXBizA3ox31YrR
Cancel-Lock: sha1:P2fz6d4p+9leYpnS/66R2D7Pgi0= sha256:VX91qU3UDJrHgoC5fhoeU1zzIZ/Be4yMZELQDjuOKC0=
X-Copyright: (C) Copyright 2023 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE-1901, en-US, it, fr-FR
 by: Stefan Ram - Thu, 3 Aug 2023 13:12 UTC

dn <PythonList@DancesWithMice.info> writes:
>Can you please explain why a multi-part second-argument must be a tuple
>and not any other form of collection-type?

|if (PyTuple_Check(cls)) {
| /* Not a general sequence -- that opens up the road to
| recursion and stack overflow. */
abstract.c

Re: isinstance()

<mailman.208.1691190453.23016.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23849&group=comp.lang.python#23849

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.neodome.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: PythonL...@DancesWithMice.info (dn)
Newsgroups: comp.lang.python
Subject: Re: isinstance()
Date: Sat, 5 Aug 2023 11:07:19 +1200
Organization: DWM
Lines: 64
Message-ID: <mailman.208.1691190453.23016.python-list@python.org>
References: <f7bd1016-ad65-2c8c-b5b2-2750632c9366@DancesWithMice.info>
<mailman.200.1691014515.23016.python-list@python.org>
<slrnuclq72.351a.jon+usenet@raven.unequivocal.eu>
<fcd09dc1-3858-5cbe-adfd-233b80550667@DancesWithMice.info>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de MWh5imthatizCa2PCHPfDAK+B/kwlIMhEKUAFaEODYCw==
Cancel-Lock: sha1:Ku7eB628BLzShnst/1nFgV96noo= sha256:Q0mL9EPMjVkWImoDb7jpxuRlFThNsSdqL+epPBWeTYc=
Return-Path: <PythonList@DancesWithMice.info>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=danceswithmice.info header.i=@danceswithmice.info
header.b=FD0cIABz; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'url-ip:140.82/16': 0.03;
'frequent': 0.05; 'underlying': 0.07; '=dn': 0.09;
'from:addr:danceswithmice.info': 0.09; 'from:addr:pythonlist':
0.09; 'mechanism': 0.09; 'ourselves': 0.09; 'proposing': 0.09;
'received:192.168.1.64': 0.09; 'say:': 0.09; 'url:github': 0.14;
'url-ip:140/8': 0.15; '(eg': 0.16; 'adults': 0.16; 'float,': 0.16;
'message-id:@DancesWithMice.info': 0.16; 'received:51.254': 0.16;
'received:51.254.211': 0.16; 'received:51.254.211.219': 0.16;
'received:cloud': 0.16; 'received:rangi.cloud': 0.16; 'recursion':
0.16; 'recursion,': 0.16; 'situations.': 0.16; 'think.': 0.16;
'tuple': 0.16; 'url:cpython': 0.16; 'wrote:': 0.16; 'python':
0.16; "can't": 0.17; 'to:addr:python-list': 0.20; "i've": 0.22;
'install': 0.23; 'seems': 0.26; '>>>': 0.28; 'header:User-
Agent:1': 0.30; 'attempt': 0.31; 'comment': 0.31;
'header:Organization:1': 0.31; 'objects': 0.32; 'python-list':
0.32; 'received:192.168.1': 0.32; 'but': 0.32; 'hold': 0.33;
'header:In-Reply-To:1': 0.34; 'complex': 0.35; 'understood': 0.35;
'following': 0.35; 'change': 0.36; 'using': 0.37;
'received:192.168': 0.37; 'thanks': 0.38; 'explain': 0.40;
'should': 0.40; 'lack': 0.60; 'likely': 0.61; 'skip:o 10': 0.61;
'ever': 0.63; 'our': 0.64; 'received:51': 0.64; 'time.': 0.66;
'that,': 0.67; 'sequence': 0.69; 'plus': 0.73; 'demand': 0.75;
'construction': 0.81; 'again:': 0.84; 'here"': 0.84; 'jon': 0.84;
'thus,': 0.84; 'heads': 0.91; 'mind,': 0.93
DKIM-Filter: OpenDKIM Filter v2.11.0 vps.rangi.cloud 9D46A237C
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=danceswithmice.info;
s=staff; t=1691190450;
bh=cSnbCE7HWDPxus7zi6pFLp7iBwNo2puGO230gUWgHfk=;
h=Date:Subject:To:References:From:In-Reply-To:From;
b=FD0cIABzoOWUFzqapjFbV1osx/yeUK345cZmxB2tZo6yt2bTy5VOAZutsQPJ1QMA/
H123uRDrIRQpI0jmnZXS/8X8+b8+VOrO4CrhQUyqDcZn9MpqfFTwpU9Bk3imPgPteC
44UGKJchv6kSL/HYA51QTxnOcrD3zsO8mMY6lX+j1WlzxPb5AeIaJI2kB0sFaRcLhQ
QPWdvRhTNmQuFqL/H/0IErOlf87TUIMpkk+3PpLMHKw/9esrFP4wZIaPlR5Ax3DKlS
nCSbZG4fr3QJMCM/Ul8TU20iOsZzElct105W+Aorhjbp1bgx7/rPLC6PDoy0Y1i+pD
OIESSaFa9rRgg==
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.13.0
Content-Language: en-GB
In-Reply-To: <slrnuclq72.351a.jon+usenet@raven.unequivocal.eu>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <fcd09dc1-3858-5cbe-adfd-233b80550667@DancesWithMice.info>
X-Mailman-Original-References: <f7bd1016-ad65-2c8c-b5b2-2750632c9366@DancesWithMice.info>
<mailman.200.1691014515.23016.python-list@python.org>
<slrnuclq72.351a.jon+usenet@raven.unequivocal.eu>
 by: dn - Fri, 4 Aug 2023 23:07 UTC

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)?

Such pondering continues in my subversive mind, given such thoughts as:

- "we're all adults here"
(eg frequent justification when arguing about Python not having "private
attributes" per-se).
So, if we introduce a complexity, on our own heads be it!
(hardly likely given that all we are likely to attempt is the
application of a simple and short 'list' of [un]acceptable types).
NB understood that the quoted-code is applied in many and other 'scalar
or tuple' situations.

- Python happily enables recursion, which "opens up the road to
recursion and stack overflow.".
So, why install a 'nanny' to save us from ourselves here?
Again: seems on-the-surface that such 'lists' (at the code-line level)
will be mono-dimensional 99.9% of the time.

NB having said that, the underlying mechanism *is* multi-dimensional:
"direct, indirect, or virtual) subclass thereof"
(https://docs.python.org/3/library/functions.html#isinstance)

Further: the 'rules' say: "classinfo is a tuple of type objects (or
recursively, other such tuples)". Thus, can write:

>>> target_object = ...
>>> inner_tuple = float, complex
>>> inner_tuple
(<class 'float'>, <class 'complex'>)
>>> isinstance( target_object, ( str, inner_tuple, int, ), )
False

I can't say I've ever used such construction in-the-wild - either
defining and then using an "inner_tuple", or even nesting. YMMV!

Any thoughts?

--
Regards,
=dn

Re: isinstance()

<mailman.209.1691191128.23016.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23850&group=comp.lang.python#23850

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: ros...@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: isinstance()
Date: Sat, 5 Aug 2023 09:18:34 +1000
Lines: 57
Message-ID: <mailman.209.1691191128.23016.python-list@python.org>
References: <f7bd1016-ad65-2c8c-b5b2-2750632c9366@DancesWithMice.info>
<mailman.200.1691014515.23016.python-list@python.org>
<slrnuclq72.351a.jon+usenet@raven.unequivocal.eu>
<fcd09dc1-3858-5cbe-adfd-233b80550667@DancesWithMice.info>
<CAPTjJmqPkBm9+ERrrL2V5a-33whQSEPMa=zwdtZv61J3Ga6e7Q@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de +TTq/EVwJk2gXxTpxqT5cQxiG7xG8DwZu7J4MVORzhiA==
Cancel-Lock: sha1:EyFB5Y1lh/td805+si1r/DCPd/U= sha256:slfHngX4Q0E4jgRXPp90kXVx4zDQu5hsgzXrly1H3Ec=
Return-Path: <rosuav@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=ZKvU9pFY;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'url-ip:140.82/16': 0.03;
'traceback': 0.04; '(most': 0.05; 'last):': 0.05; '2023': 0.07;
'aug': 0.07; 'thing.': 0.07; 'proposing': 0.09; 'typeerror:':
0.09; 'url:github': 0.14; 'url-ip:140/8': 0.15; 'chrisa': 0.16;
'division': 0.16; 'ensuring': 0.16; 'from:addr:rosuav': 0.16;
'from:name:chris angelico': 0.16; 'iterate': 0.16; 'notes,': 0.16;
'objects.': 0.16; 'recursion': 0.16; 'stuck': 0.16; 'think.':
0.16; 'tuple': 0.16; 'tuples': 0.16; 'tuples.': 0.16;
'url:cpython': 0.16; 'wrote:': 0.16; 'values': 0.17; "can't":
0.17; 'to:addr:python-list': 0.20; 'option': 0.20; 'language':
0.21; 'exception': 0.22; 'sat,': 0.22; 'seems': 0.26; 'classes':
0.26; '>>>': 0.28; 'comment': 0.31; "doesn't": 0.32; '"",': 0.32;
'python-list': 0.32; 'zero': 0.32; 'message-id:@mail.gmail.com':
0.32; 'but': 0.32; 'there': 0.33; 'hold': 0.33; 'core': 0.34;
'skip:" 20': 0.34; 'header:In-Reply-To:1': 0.34;
'received:google.com': 0.34; 'handling': 0.35; 'following': 0.35;
'from:addr:gmail.com': 0.35; 'change': 0.36; "it's": 0.37; 'file':
0.38; 'means': 0.38; 'thanks': 0.38; 'quite': 0.39; 'single':
0.39; 'list': 0.39; 'explain': 0.40; 'otherwise,': 0.40;
'serious': 0.40; 'should': 0.40; 'lack': 0.60; 'above': 0.62;
'skip:z 10': 0.62; 'skip:b 10': 0.63; 'pass': 0.64; 'during':
0.69; 'sequence': 0.69; 'plus': 0.73; 'demand': 0.75; 'features':
0.75; '....': 0.76; 'catch': 0.84; 'catching': 0.84; 'inherit':
0.84; 'jon': 0.84; 'worry': 0.95
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20221208; t=1691191126; x=1691795926;
h=to:subject:message-id:date:from:in-reply-to:references:mime-version
:from:to:cc:subject:date:message-id:reply-to;
bh=T995JeeZxYIUsMswyGk3idgNKTCwKUAg7TLjiK2617E=;
b=ZKvU9pFYNjIvQrUDoQa1JalXo/T/B6zGW64CECavPam3s2sFx+CGYw7OWOt8vNPIfM
CxrBCq6/jIhrRixE5DOmNJ//TYiZvK/uTOB6RSedOtOzoz/djhBDDJtWurEX75Meb6Pm
GFLXSWkelhSD02PK3rixccjyx5WmbGixqLjPyLRyjAzyIWd7Gy4z3Zg0SNuadHKu/w9w
EuB81mNSc6WCqc5Qcm3eV0/AXGr5WA0IGwZLrR2sjNNB1BJe42+u6kT+YL6REK5k9iwX
YEuUuP5TiGl82L/hr7vZM8UClEugJnH/uEBuD2mbIATHh8DIk492xj5CGqPahEVqcp+9
g4sg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20221208; t=1691191126; x=1691795926;
h=to:subject:message-id:date:from:in-reply-to:references:mime-version
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
bh=T995JeeZxYIUsMswyGk3idgNKTCwKUAg7TLjiK2617E=;
b=giC2YyB6MntoYX+dth2VuZVndjeB0t8WuonbWSLfH+2ukWjl/lYklhcJ7sIkPmiMQU
bXwJlc+cfmffvjfU9IOKku13lYjaWI//WeDpgiLVf0wCMVKD1LfFpJd18Pi3L0V9fXan
oVE/3h0jdml3/4oXywNjMViNshXTIK14AxtU/2JuzL3wiK5XbVyhh7cASpB96VXd52eT
/UNgX6WMWbH5a/MBx15teR6ExCbL45Dk9v7AanFvMpC+6V9viVmtOPkN6G+wSQwzQ2aQ
r1Y8Gye5JJDjtD1eS9nBv+sLNEd1JgxTUH2AGiikGPxttHVoVNpk23d74L7cZj+rUqFa
zh7A==
X-Gm-Message-State: AOJu0YxydgC3cGgXjdDEyoErhPTvt22eA2zIq0LRZ8I3CN2ONjBguIIb
EWrzrw32139jf+E2XWZFvo/49COdSTg4U9lkuw/YqlWykmk=
X-Google-Smtp-Source: AGHT+IEA5j07YUAW01KgJ+1317QpqW7M3TQauwXnwRbHJSX89nZ0w/uBNboIXZWj989oQxbiYpfM1b1c/3DHlL73NiU=
X-Received: by 2002:ac2:5192:0:b0:4f8:4216:e91f with SMTP id
u18-20020ac25192000000b004f84216e91fmr1889003lfi.63.1691191125882; Fri, 04
Aug 2023 16:18:45 -0700 (PDT)
In-Reply-To: <fcd09dc1-3858-5cbe-adfd-233b80550667@DancesWithMice.info>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <CAPTjJmqPkBm9+ERrrL2V5a-33whQSEPMa=zwdtZv61J3Ga6e7Q@mail.gmail.com>
X-Mailman-Original-References: <f7bd1016-ad65-2c8c-b5b2-2750632c9366@DancesWithMice.info>
<mailman.200.1691014515.23016.python-list@python.org>
<slrnuclq72.351a.jon+usenet@raven.unequivocal.eu>
<fcd09dc1-3858-5cbe-adfd-233b80550667@DancesWithMice.info>
 by: Chris Angelico - Fri, 4 Aug 2023 23:18 UTC

On Sat, 5 Aug 2023 at 09:08, dn via Python-list <python-list@python.org> wrote:
>
> 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.

>>> "spam".startswith(["sp", "h"])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: startswith first arg must be str or a tuple of str, not list
>>> try: 1/0
.... except [ValueError, IndexError]: pass
....
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.)

ChrisA

Re: isinstance()

<mailman.210.1691192120.23016.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23851&group=comp.lang.python#23851

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: PythonL...@DancesWithMice.info (dn)
Newsgroups: comp.lang.python
Subject: Re: isinstance()
Date: Sat, 5 Aug 2023 11:35:03 +1200
Organization: DWM
Lines: 84
Message-ID: <mailman.210.1691192120.23016.python-list@python.org>
References: <f7bd1016-ad65-2c8c-b5b2-2750632c9366@DancesWithMice.info>
<mailman.200.1691014515.23016.python-list@python.org>
<slrnuclq72.351a.jon+usenet@raven.unequivocal.eu>
<fcd09dc1-3858-5cbe-adfd-233b80550667@DancesWithMice.info>
<CAPTjJmqPkBm9+ERrrL2V5a-33whQSEPMa=zwdtZv61J3Ga6e7Q@mail.gmail.com>
<4cf7e053-95fa-4ece-1a77-3b2b8421a11c@DancesWithMice.info>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de TM323bHrN8YSColFJW3iCwA6w0hbm/ac/Azg488BhYng==
Cancel-Lock: sha1:kMHcygFdb5dCebeZQdkkxO6OtTw= sha256:Ks4mFRu2/7fVIjQUU59K/8U7w0sETx82PT9zdtr2k/I=
Return-Path: <PythonList@DancesWithMice.info>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=danceswithmice.info header.i=@danceswithmice.info
header.b=qE05ILIV; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'url-ip:140.82/16': 0.03;
'(which': 0.04; 'argument': 0.04; 'traceback': 0.04; '(most':
0.05; 'last):': 0.05; '2023': 0.07; 'aug': 0.07; 'thing.': 0.07;
'=dn': 0.09; 'angelico': 0.09; 'faced': 0.09; 'fails': 0.09;
'from:addr:danceswithmice.info': 0.09; 'from:addr:pythonlist':
0.09; 'instances': 0.09; 'proposing': 0.09;
'received:192.168.1.64': 0.09; 'situation,': 0.09; 'typeerror:':
0.09; 'yes.': 0.09; 'url:github': 0.14; 'url-ip:140/8': 0.15;
'(because': 0.16; '>>>>': 0.16; 'division': 0.16; 'ensuring':
0.16; 'iterable': 0.16; 'iterate': 0.16; 'message-
id:@DancesWithMice.info': 0.16; 'notes,': 0.16; 'objects.': 0.16;
'oop': 0.16; 'received:cloud': 0.16; 'received:rangi.cloud': 0.16;
'recursion': 0.16; 'stuck': 0.16; 'think.': 0.16; 'tuple': 0.16;
'tuples': 0.16; 'tuples.': 0.16; 'url:cpython': 0.16; 'wrote:':
0.16; 'python': 0.16; 'values': 0.17; "can't": 0.17; 'to:addr
:python-list': 0.20; 'option': 0.20; 'language': 0.21;
'exception': 0.22; 'sat,': 0.22; 'idea': 0.24; 'actual': 0.25;
'seems': 0.26; 'classes': 0.26; '>>>': 0.28; 'chris': 0.28;
'thinking': 0.28; 'header:User-Agent:1': 0.30; 'comment': 0.31;
'header:Organization:1': 0.31; "doesn't": 0.32; '"",': 0.32;
'python-list': 0.32; 'zero': 0.32; 'received:192.168.1': 0.32;
'but': 0.32; "i'll": 0.33; 'there': 0.33; 'hold': 0.33; 'core':
0.34; 'skip:" 20': 0.34; 'header:In-Reply-To:1': 0.34; 'handling':
0.35; 'following': 0.35; 'change': 0.36; "it's": 0.37;
'received:192.168': 0.37; 'file': 0.38; 'means': 0.38; 'thanks':
0.38; 'quite': 0.39; 'single': 0.39; 'mentioned': 0.39; 'list':
0.39; 'use': 0.39; 'still': 0.40; 'explain': 0.40; 'otherwise,':
0.40; 'serious': 0.40; 'situation': 0.40; 'both': 0.40; 'should':
0.40; 'lack': 0.60; 'above': 0.62; 'skip:z 10': 0.62; 'skip:b 10':
0.63; 'pass': 0.64; 'earlier': 0.67; 'during': 0.69; 'sequence':
0.69; 'plus': 0.73; 'demand': 0.75; 'features': 0.75; 'catch':
0.84; 'catching': 0.84; 'inherit': 0.84; 'jon': 0.84; 'brain':
0.91; 'worry': 0.95
DKIM-Filter: OpenDKIM Filter v2.11.0 vps.rangi.cloud 623A7237C
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=danceswithmice.info;
s=staff; t=1691192112;
bh=d5zH6EuftFLAqt2NQaSCSPbkwPMP1DRrz8lk7ie/vLA=;
h=Date:Subject:To:References:From:In-Reply-To:From;
b=qE05ILIVuYvmmGRzF3LPFyQVDuqIke6XnOP98wLbQtyhVWfqLW0/W3MhXDXtuZtqg
4Hx8ui0mDuYyHGD5WIEqCB/nlxS2Z95xTWWzKlLfLFpRSbrJJZCBZfZf4yXn9fjAB7
xZBde4FyLJVS6mLTczG3X+nlsX4G9afs+tu70sfhW4dh+b4k86lyZ2KbmC2nVtjSIf
ddtLW+bw0K+aSWgXdq5k3wjt8K/hWPEc3zJl1+afmU5fXiMbaw0wQZ4wodqJE9JtRS
IJ8pO5TS/dRhWu4poNZfgOniHSognO2rItAYXbaW8yxWWTw5axQx13M/65H/Twpnd1
450vUqhksIS1Q==
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.13.0
Content-Language: en-GB
In-Reply-To: <CAPTjJmqPkBm9+ERrrL2V5a-33whQSEPMa=zwdtZv61J3Ga6e7Q@mail.gmail.com>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <4cf7e053-95fa-4ece-1a77-3b2b8421a11c@DancesWithMice.info>
X-Mailman-Original-References: <f7bd1016-ad65-2c8c-b5b2-2750632c9366@DancesWithMice.info>
<mailman.200.1691014515.23016.python-list@python.org>
<slrnuclq72.351a.jon+usenet@raven.unequivocal.eu>
<fcd09dc1-3858-5cbe-adfd-233b80550667@DancesWithMice.info>
<CAPTjJmqPkBm9+ERrrL2V5a-33whQSEPMa=zwdtZv61J3Ga6e7Q@mail.gmail.com>
 by: dn - Fri, 4 Aug 2023 23:35 UTC

On 05/08/2023 11.18, Chris Angelico via Python-list wrote:
> On Sat, 5 Aug 2023 at 09:08, dn via Python-list <python-list@python.org> wrote:
>>
>> 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.
>
>>>> "spam".startswith(["sp", "h"])
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: startswith first arg must be str or a tuple of str, not list
>>>> try: 1/0
> ... except [ValueError, IndexError]: pass
> ...
> 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.)
Yes. Thanks Chris!

The idea that such 'lists' be immutable (even, hashable), and therefore
a tuple, makes a certain amount of sense, and @Cameron mentioned
'frugality'.

My limitation is thinking only at the Python level (which as @Jon
pointed-out, is only part of the story).

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. Alternately, in an OOP situation, utilise
polymorphism so that the 'scalar' and 'iterable' instances both include
the appropriate method[name]. Accordingly, as long as the argument is an
iterable (includes an __iter__() method), the actual type/class is
more-or-less irrelevant.

However, as observed earlier - and with these additions, having learned
the 'rule', ie use a tuple; the brain is still pondering, but have
shrugged-shoulders and carried-on regardless...

--
--
Regards,
=dn

Re: isinstance()

<mailman.211.1691192370.23016.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23852&group=comp.lang.python#23852

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: ros...@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: isinstance()
Date: Sat, 5 Aug 2023 09:39:16 +1000
Lines: 11
Message-ID: <mailman.211.1691192370.23016.python-list@python.org>
References: <f7bd1016-ad65-2c8c-b5b2-2750632c9366@DancesWithMice.info>
<mailman.200.1691014515.23016.python-list@python.org>
<slrnuclq72.351a.jon+usenet@raven.unequivocal.eu>
<fcd09dc1-3858-5cbe-adfd-233b80550667@DancesWithMice.info>
<CAPTjJmqPkBm9+ERrrL2V5a-33whQSEPMa=zwdtZv61J3Ga6e7Q@mail.gmail.com>
<4cf7e053-95fa-4ece-1a77-3b2b8421a11c@DancesWithMice.info>
<CAPTjJmrZN+HwoTyXc_FiHo9TpE0WvXTwd2ZJdRo1=5dkpfV6HA@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de kswsJkmG6EoCNtTn63y2lgG2vJfm/iCNhcqPgAh1eQ2Q==
Cancel-Lock: sha1:Iv6N8RErompH1II5p5ZRaWL3vM4= sha256:S0lfcr9qZ7vY+MTU573v9850XtoSB3zG/iduC+96xOI=
Return-Path: <rosuav@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=Wm7GIDaC;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'argument': 0.04; '2023':
0.07; 'aug': 0.07; 'string': 0.07; 'faced': 0.09; 'fails': 0.09;
'(because': 0.16; 'chrisa': 0.16; 'from:addr:rosuav': 0.16;
'from:name:chris angelico': 0.16; 'wrote:': 0.16; "aren't": 0.19;
'to:addr:python-list': 0.20; 'sat,': 0.22; 'python-list': 0.32;
'message-id:@mail.gmail.com': 0.32; "i'll": 0.33; 'header:In-
Reply-To:1': 0.34; 'received:google.com': 0.34;
'from:addr:gmail.com': 0.35; 'handle': 0.39; 'list': 0.39; 'use':
0.39; 'situation': 0.40; 'great': 0.63
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20221208; t=1691192368; x=1691797168;
h=to:subject:message-id:date:from:in-reply-to:references:mime-version
:from:to:cc:subject:date:message-id:reply-to;
bh=yJOL4W+rHRQozm/+Y0B1fuejH+28hd3+0nGWqMmYeII=;
b=Wm7GIDaCIbOooWmFdeHzlOSI/tBud3I6TyXVkaw9zob/aj7H1xvxst+EluaEjKEXcR
OI+JCZq2clMWZlofzy1jIiOKdpD4ovFL9HZQLwTut5oCFHfIMXXVpQr5SCyoy28yOQR9
i+pRA1UX43RaT1Wg6pNQRElRwUiWukpUzAXh3J0JkpHS+Xku4i0qVE0g0LgntJi9uzqC
ohnqoCLBPFJkXhg+pGEUWHh6yibQ/2V499Mh1dWKULr5LvUfAgca4PzoZsyfKg9NYQLA
M5AxKbG3HH9UJAi83JOaZkuSihbfm4oUX3BjVr2Lt4fCpEPomDYkmDTijGTHCOEgfYSz
gocQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20221208; t=1691192368; x=1691797168;
h=to:subject:message-id:date:from:in-reply-to:references:mime-version
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
bh=yJOL4W+rHRQozm/+Y0B1fuejH+28hd3+0nGWqMmYeII=;
b=CDeZyoJwlY+M5g7xDMqnccF6ea7a4OioR8RDA6R+Pa7Zo9WyL5dgAvVbfNEDZt4qtC
SnxsjLDO4LnpUhB5J+hNR4bS9b27hpDzylC79ZCNR4p4lEpqy/RrQDCYHVuA9UNRbudL
kS5DV8qEXXTAK49Ly3waVw84f86GV325d3UeJWaW8gnweElfZtdop+ytBYeaPTf0+R/V
Bv28bHWMAKiC7j5913zhFtpizbzSKNSGqU5lHCpNZIIo0bhJh5SstPdcDJsKMZk0nmQl
dLyiFzM5Kx2fjkmwvkNp6JCqWolGctw+VZnxHii3BjMgem/t/qdjDbF2Hyfq9JHgKERa
fPxQ==
X-Gm-Message-State: AOJu0YwJTYTY0tE3GV0Jq+P40OQOwVlOAhOxQ8kVsJtygaNBB3+zIONQ
OzOoc+NdYhS0HkDvM4NUfLG2aTyXIYiLVQ+x5d2nONot
X-Google-Smtp-Source: AGHT+IHDaDztmRGKt0FXB1ztkFg5AMSvRdA7F2/prYIGBaWzJ4/6QMfbH13g+5AmC24Y3pNysmHDdU0wgpwCwkVQ90Q=
X-Received: by 2002:a05:6512:ad5:b0:4fb:9050:1d92 with SMTP id
n21-20020a0565120ad500b004fb90501d92mr2324206lfu.51.1691192367849; Fri, 04
Aug 2023 16:39:27 -0700 (PDT)
In-Reply-To: <4cf7e053-95fa-4ece-1a77-3b2b8421a11c@DancesWithMice.info>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <CAPTjJmrZN+HwoTyXc_FiHo9TpE0WvXTwd2ZJdRo1=5dkpfV6HA@mail.gmail.com>
X-Mailman-Original-References: <f7bd1016-ad65-2c8c-b5b2-2750632c9366@DancesWithMice.info>
<mailman.200.1691014515.23016.python-list@python.org>
<slrnuclq72.351a.jon+usenet@raven.unequivocal.eu>
<fcd09dc1-3858-5cbe-adfd-233b80550667@DancesWithMice.info>
<CAPTjJmqPkBm9+ERrrL2V5a-33whQSEPMa=zwdtZv61J3Ga6e7Q@mail.gmail.com>
<4cf7e053-95fa-4ece-1a77-3b2b8421a11c@DancesWithMice.info>
 by: Chris Angelico - Fri, 4 Aug 2023 23:39 UTC

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.

ChrisA

Re: isinstance()

<mailman.212.1691203061.23016.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23853&group=comp.lang.python#23853

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: grant.b....@gmail.com (Grant Edwards)
Newsgroups: comp.lang.python
Subject: Re: isinstance()
Date: Fri, 04 Aug 2023 19:37:37 -0700 (PDT)
Lines: 34
Message-ID: <mailman.212.1691203061.23016.python-list@python.org>
References: <f7bd1016-ad65-2c8c-b5b2-2750632c9366@DancesWithMice.info>
<mailman.200.1691014515.23016.python-list@python.org>
<slrnuclq72.351a.jon+usenet@raven.unequivocal.eu>
<fcd09dc1-3858-5cbe-adfd-233b80550667@DancesWithMice.info>
<CAPTjJmqPkBm9+ERrrL2V5a-33whQSEPMa=zwdtZv61J3Ga6e7Q@mail.gmail.com>
<4cf7e053-95fa-4ece-1a77-3b2b8421a11c@DancesWithMice.info>
<CAPTjJmrZN+HwoTyXc_FiHo9TpE0WvXTwd2ZJdRo1=5dkpfV6HA@mail.gmail.com>
<64cdb5f1.6b0a0220.e2a02.1c1c@mx.google.com>
X-Trace: news.uni-berlin.de 1H3WmFptlklGJbqawuNS3AV8uk8hvhTtHFGamG5nvG6g==
Cancel-Lock: sha1:mRKV3aCPxROgkG6/xf11zhkU8lQ= sha256:ibNnHskX29uWPVNr++SCKZ+6z7Rs8p+M6E33TDYlIcY=
Return-Path: <grant.b.edwards@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=p2ReSQh7;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'argument': 0.04; '2023':
0.07; 'aug': 0.07; 'string': 0.07; 'angelico': 0.09; 'faced':
0.09; 'fails': 0.09; 'obviously': 0.09; 'received:mail-
io1-xd33.google.com': 0.09; 'spanish': 0.09; '(because': 0.16;
'anyway.': 0.16; 'from:addr:grant.b.edwards': 0.16;
'from:name:grant edwards': 0.16; 'strings,': 0.16; 'wrote:': 0.16;
'problem': 0.16; 'grant': 0.17; "aren't": 0.19; 'to:addr:python-
list': 0.20; 'sat,': 0.22; "wasn't": 0.26; 'chris': 0.28; 'header
:User-Agent:1': 0.30; 'python-list': 0.32; 'but': 0.32; "i'll":
0.33; 'received:google.com': 0.34; 'from:addr:gmail.com': 0.35;
"it's": 0.37; 'handle': 0.39; 'list': 0.39; 'use': 0.39;
'situation': 0.40; 'should': 0.40; 'great': 0.63; 'times.': 0.64;
'that,': 0.67; 'happens': 0.84; 'strings': 0.84; 'words:': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20221208; t=1691203058; x=1691807858;
h=to:user-agent:references:subject:from:date:message-id:from:to:cc
:subject:date:message-id:reply-to;
bh=bcYw8vnT87IH9BqUvzGCPWhz0troitFZkyWaTNyeSyk=;
b=p2ReSQh7BvtypJeJ/GPXXOtsmijgfw2ajEyHblTfx8J2d2STDYpnF53KfNMdKnGQcp
mqpdZovgkBW4vcaQIT8t29U9VD8reiCB7eFsyMIGltWut7ioTRJuEzdn1cC6g+tGoDva
2pbvIqXb4uoonZ5cWddeHTmnzeoFgEllOSW8+k/iIwR/r6WnZgDrMbusF8tim+kVJguJ
6SG0gE7E6kBSJB2wBDxbFREQmnR4CYk0WSzK0hc1Lx3imqYXpSPvDIPev7eOYOVqDJ61
Db7yGUhi5ZwlpgwsW14tgEauOBM2ePfIgpKTr6EosXPPGlb5UYFTBoJEuornKB9zLcdO
vwUg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20221208; t=1691203058; x=1691807858;
h=to:user-agent:references:subject:from:date:message-id
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
bh=bcYw8vnT87IH9BqUvzGCPWhz0troitFZkyWaTNyeSyk=;
b=K2JeuE1pF1UW9Svo4UvpsDveJ38CqpEKB+/ewxe1C5yzeJr8rhWkgZ77NwXCN+A6dz
8sz5V+1ArClRHGWfXc/1ljPk8OZErN3Lby+9AytikG67CV4aXwcYApp877HMzOkL9pFY
vKxCCn/ezGubJho0kxX6DJacKczrNrE0Bhx3KER125hOPB06Zq+2ZVfLuH9+KnjJOna8
o3NX0lOl/N5Paj9hQ8HCXPmOJrOTp7JHZGSMQ17QmLRp7osYiumTKaH6MKRBQZIw7sXp
1W+9RtrBXCPUNu+IJ9Ow9qkpJeBR1VziFCFZ6bISTKdDh7FiS+T7BKasALQHTdLqEJ27
+uzw==
X-Gm-Message-State: AOJu0YwZOnBlpDcxqepHHj9VmM76dLEjMdxaNd4Q4JjgWdchA24qw44F
Kj8YIvkyUaor6fwcqemeG/QfV65/RE8=
X-Google-Smtp-Source: AGHT+IEqzRONoamF+1RhrT6aIyZm2Jqu8n4TZpcMuYcXYWuZwCUCHHogUrQppiyegQRhclXEL74u3A==
X-Received: by 2002:a6b:ed11:0:b0:786:f352:e3d4 with SMTP id
n17-20020a6bed11000000b00786f352e3d4mr5040804iog.7.1691203058318;
Fri, 04 Aug 2023 19:37:38 -0700 (PDT)
User-Agent: slrn/1.0.3 (Linux)
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <64cdb5f1.6b0a0220.e2a02.1c1c@mx.google.com>
X-Mailman-Original-References: <f7bd1016-ad65-2c8c-b5b2-2750632c9366@DancesWithMice.info>
<mailman.200.1691014515.23016.python-list@python.org>
<slrnuclq72.351a.jon+usenet@raven.unequivocal.eu>
<fcd09dc1-3858-5cbe-adfd-233b80550667@DancesWithMice.info>
<CAPTjJmqPkBm9+ERrrL2V5a-33whQSEPMa=zwdtZv61J3Ga6e7Q@mail.gmail.com>
<4cf7e053-95fa-4ece-1a77-3b2b8421a11c@DancesWithMice.info>
<CAPTjJmrZN+HwoTyXc_FiHo9TpE0WvXTwd2ZJdRo1=5dkpfV6HA@mail.gmail.com>
 by: Grant Edwards - Sat, 5 Aug 2023 02:37 UTC

On 2023-08-04, Chris Angelico via Python-list <python-list@python.org> wrote:
> 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.

If you do that, you're obviously not expecting to handle strings. The
problem happens when you're not expecting to handle strings, and you
get passed one anyway.

It's like the Spanish Inquisition...

> The string "spam" is sometimes equivalent to the list ["s", "p",
> "a", "m"] and sometimes not.

And b"ABCD" is sometimes equivalent to the list [65,66,67,68] and
sometimes not.

Been there, fell in that hole.

More than a few times. :/

Famous Last Words: "I wasn't expecting to handle strings -- but I
should have been..."

--
Grant

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor