Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

19 May, 2024: Line wrapping has been changed to be more consistent with Usenet standards.
 If you find that it is broken please let me know here rocksolid.nodes.help


devel / comp.lang.python / Re: type annotation vs working code

SubjectAuthor
o type annotation vs working codeKarsten Hilbert

1
Re: type annotation vs working code

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

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=24322&group=comp.lang.python#24322

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: Karsten.Hilbert@gmx.net (Karsten Hilbert)
Newsgroups: comp.lang.python
Subject: Re: type annotation vs working code
Date: Sun, 1 Oct 2023 00:25:11 +0200
Lines: 77
Sender: <karsten.hilbert@gmx.net>
Message-ID: <mailman.374.1696112714.23016.python-list@python.org>
References: <ZRhwWN29i3mskxEL@hermes.hilbert.loc>
<ecc36615-1844-455f-b701-2bd41d98e9e1@DancesWithMice.info>
<ZRigRwHpNReUo5rF@hermes.hilbert.loc>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de Wpk7034dUFGWsVtVaRn/6gg+2dIxNgLV5Rt3ES4wZz9Q==
Cancel-Lock: sha1:V0JNn8O8Mh6M/IT/g04tsTHFUOE= sha256:NeADGD7jzWI9nKkyD2EcXSrvl76YnsQVPGE5nw51/OY=
Return-Path: <karsten.hilbert@gmx.net>
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=gmx.net header.i=karsten.hilbert@gmx.net
header.b=jsVTp2dK; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.004
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'def': 0.04; 'pycharm':
0.04; '2023': 0.07; 'received:212.227': 0.07; 'sun,': 0.07; ':-)':
0.09; 'expression': 0.09; 'gpg': 0.09; 'karsten': 0.09;
'question:': 0.09; 'schrieb': 0.09; '(presumably)': 0.16;
'(without': 0.16; '1713': 0.16; 'akin': 0.16; 'attributes': 0.16;
'declare': 0.16; 'generating': 0.16; 'idiom': 0.16; 'leveraged':
0.16; 'received:212.227.15': 0.16; 'subject:code': 0.16;
'subject:working': 0.16; 'to:addr:python-list': 0.20; 'language':
0.21; 'skip:_ 10': 0.22; 'skip:p 30': 0.23; '(and': 0.25;
'cannot': 0.25; 'seems': 0.26; "isn't": 0.27; 'fact': 0.28;
'purpose': 0.28; 'thinking': 0.28; 'error': 0.29; 'wondering':
0.31; "doesn't": 0.32; 'but': 0.32; 'same': 0.34; 'work.': 0.34;
'header:In-Reply-To:1': 0.34; "skip:' 10": 0.37; 'using': 0.37;
'class': 0.37; 'way': 0.38; 'define': 0.40; 'me.': 0.62; 'point.':
0.62; 'received:212': 0.62; 'true': 0.63; 'simply': 0.63;
'between': 0.63; 'your': 0.64; 'generally': 0.67; 'matter': 0.68;
'response.': 0.68; 'etc,': 0.69; 'skip:w 20': 0.69; '....': 0.76;
'subsequent': 0.76; 'code)': 0.84; 'surprised': 0.84; 'exists':
0.91; 'false;': 0.91; 'subject:type': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=s31663417;
t=1696112712; x=1696717512; i=karsten.hilbert@gmx.net;
bh=OY3MxrhtMrs1Z56bGDgG3x/1tLor+mWjCO9FjE3ZztE=;
h=X-UI-Sender-Class:Date:From:To:Subject:References:In-Reply-To;
b=jsVTp2dK56ApfSzQYBUtYtoCYBFSeocOIXVTiEf2CE4akG/blPCZbERTfSXx74MlFfwnNcr2OBt
DBlTZZU2TO7NR/lr6T8VUuUmxEIleS0UPNFsz/R2bDuCmUBoAmtNC3dXETLiiyEwrOmnfhvBkne8d
AHVu+oe7yZFJUsTwgurgJSl+LrUslpxrMy0+JHv/tAO+hLZtAzFMmMDdKAO4sFjxeF+b39rxlaZYD
TIZdYmEV91Ggxx1aESLO1o5pXZvMc5LEtjV2JfGGFHj2OMpXv3poKhxhHVO9QvzvxGJnoFhH7gyfS
HWUjwegYFFlUDVZtcKHaz8jV0rXElXUCWG5Q==
X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a
Content-Disposition: inline
In-Reply-To: <ecc36615-1844-455f-b701-2bd41d98e9e1@DancesWithMice.info>
Ma_X_il-Followup-to: d
Re_X_turn-receipt-to: Karsten.Hilbert@gmx.net
Di_X_sposition-Notification-To: Karsten.Hilbert@gmx.net
X-Confi_X_rm-Reading-To: Karsten.Hilbert@gmx.net
X-Pri_X_ority: 2 (High)
X-Provags-ID: V03:K1:kOgaA1UpuUw9CBQIcpSVdXDpykWwbAXFX+pgKdufS8kA2IAbAh2
46HEvewYYA0IkbStD8+Su6rThLpWHyPI8gwEh4Pfh1rdU9kUCEEDOnq7SEzOgj+wNnvJroo
bqJPtxz8J1dI+cuU/o9VS4vEWZ+nOWojtQ0o4udQIG71D2GN3beGdN89OxSwSKpCrd4IgGE
zhZrSH99B7H4XFdCgz4/Q==
X-Spam-Flag: NO
UI-OutboundReport: notjunk:1;M01:P0:2Aw/wgIiNEE=;HJPI7mGFQCIIGXgNmgw/eJxPl2o
QR2Ct5sW8ef7ychGE7xnDQnJhpyx0Hu8A0Y1JoZrj990CwVDmnLHvvloYFZDYWkjjDERkAq6V
l5POw4pJGxl68GBOLgYXgcd8YXThFYy8kZg6Ae/4B27GDNDhYWgttA15D49A5q1fkokFU0F21
cULK2onIa+xDljQONwA3BvrPhifmmmcWPFjs7VG+wuMDDwXvGKI43tzZK2DnksldKZQ292Ghr
XXfNuzhJsueSVBJMzQWkVNimp20Z7utD/KMiTYd8rlj1KIUo//txR38d5Yc76JmyOzYItHfi7
HuMMz4JSt8l3fK5dsmXOw5m4NwQF31oKo+msNIo5BTi3cOXSQA05Qa69ReyZ//rWzgR6qi2Nb
hsJfjpMmter8rKjXuOf9zgvRCI0WEJOYTPX6GR8fb3FgpCq1LIcP3Zqyp6BiHpczm+KQX+4rM
yESZhdvqP91Vniz2mszYhgHnSKzNRjAIeF5ze5qZl7IHib8lVNENdNoF9YODa6ns3Ore8mpNj
+paSAikdD0AZWx7MRBG/ItcMfaMrrtdvbIGLWJ6BSKw+d5gPZuT5OMTKfRr9DgrvYGRjxwVUK
AHzfnUxrOpaIhni4oQPWkFyMfwGoGoX4En/iNZ1mPVNvbxJD/yUOcbHY8p+c7pGKZnTdoZue1
1kjt7GTjPJ9n60M9omZldXl/RsKIri+OO95Xl+EpmVg5VrC+TJfO57+VXlpdz4W5E+zb5oUeb
WFAclh3FmNd1GBbphRwA+sw4xv78wE/4uchqDY+FvbiC6BMIIGzWwB4y7nypc/snYJgz91qFb
emPpIZkK9A5cs/0qYhXSqMll3IqiXvDG63hSdpB195c4IcreUhyVkCBhUnWIHsIEVlIHDGKBO
7wWJlvkHPavEt75WK6vjRityIArFsyWXGP+V/9gAlI6dBSSsIBoYWGvQfNAaw8/HJpD7LWUm/
XbQqx/7sskLyOo7Yz7pBdEk1Z6I=
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: <ZRigRwHpNReUo5rF@hermes.hilbert.loc>
X-Mailman-Original-References: <ZRhwWN29i3mskxEL@hermes.hilbert.loc>
<ecc36615-1844-455f-b701-2bd41d98e9e1@DancesWithMice.info>
 by: Karsten Hilbert - Sat, 30 Sep 2023 22:25 UTC

Am Sun, Oct 01, 2023 at 09:04:05AM +1300 schrieb dn via Python-list:

> >class WorkingSingleton(Borg):
> >
> > def __init__(self):
> > print(self.__class__.__name__, ':')
> > try:
> > self.already_initialized
> > print('already initialized')
> > return
> >
> > except AttributeError:
> > print('initializing')
> >
> > self.already_initialized = True
> > self.special_value = 42

> >Where's the error in my thinking (or code) ?
>
> What is your thinking?
> Specifically, what is the purpose of testing self.already_initialized?

The purpose is to check whether the singleton class has been
.... initialized :-)

The line

self.already_initialized = True

is misleading as to the fact that it doesn't matter at all
what self.already_initialized is set to, as long as is
exists for the next time around.

> Isn't it generally regarded as 'best practice' to declare (and define a value for) all
> attributes in __init__()? (or equivalent) In which case, it will (presumably) be defined
> as False; and the try-except reworded to an if-else.

I fail to see how that can differentiate between first-call
and subsequent call.

> Alternately, how about using hasattr()? eg
>
> if hasattr( self.already_initialized, 'attribute_name' ):

That does work. I am using that idiom in other children of
Borg. But that's besides the point. I was wondering why it
does not work the same way with and without the type
annotation.

> try:
> self.already_initialized
>
> line is flagged by the assorted linters, etc, in my PyCharm as:
>
> Statement seems to have no effect.

Well, the linter simply cannot see the purpose, which is
test-of-existence.

> Question: is it a legal expression (without the typing)?

It borders on the illegal, I suppose, as the self-
introspection capabilities of the language are being
leveraged to achieve a legal purpose.

Which seems akin constructs for generating compatibility
between versions.

It seems the answer is being pointed to in Matts response.

It just mightily surprised me.

Karsten
--
GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B


devel / comp.lang.python / Re: type annotation vs working code

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor