----------------------------------------------------------------------------------
@MSGID:
<mailman.374.1696112714.23016.python-list@python.org> ad78a412
@REPLY: <ZRigRwHpNReUo5rF@hermes.hilbert.loc>
8f39848f
@REPLYADDR Karsten Hilbert
<Karsten.Hilbert@gmx.net>
@REPLYTO 2:5075/128 Karsten Hilbert
@CHRS: CP866 2
@RFC: 1 0
@RFC-Message-ID:
<mailman.374.1696112714.23016.python-list@python.org>
@RFC-References: <ZRhwWN29i3mskxEL@hermes.hilbert.loc>
<ecc36615-1844-455f-b701-2bd41d98e9e1@DancesWithMice.info> <ZRigRwHpNReUo5rF@hermes.hilbert.loc>
@TZUTC: 0200
@TID: FIDOGATE-5.12-ge4e8b94
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
--- FIDOGATE 5.12-ge4e8b94
* Origin: usenet.network (2:5075/128)
SEEN-BY: 50/109 301/1 467/888 4500/1 5000/111
5001/100 5005/49 5020/715 830
SEEN-BY: 5020/848 1042 4441 12000 5030/49 1081
5054/8 5061/133 5075/128
SEEN-BY: 5080/102 5083/1 444
@PATH: 5075/128 5020/1042 4441