Nп/п : 89 из 100
 От   : Karsten Hilbert                     2:5075/128        30 сен 23 21:00:40
 К    : All                                                   30 сен 23 22:04:03
 Тема : type annotation vs working code
----------------------------------------------------------------------------------
                                                                                 
@MSGID:
<mailman.371.1696100442.23016.python-list@python.org> 2377407b
@REPLY: <ZRhwWN29i3mskxEL@hermes.hilbert.loc>
82d15bc3
@REPLYADDR Karsten Hilbert
<Karsten.Hilbert@gmx.net>
@REPLYTO 2:5075/128 Karsten Hilbert
@CHRS: CP866 2
@RFC: 1 0
@RFC-Message-ID:
<mailman.371.1696100442.23016.python-list@python.org>
@RFC-References:
<ZRhwWN29i3mskxEL@hermes.hilbert.loc>
@TZUTC: 0200
@TID: FIDOGATE-5.12-ge4e8b94
A type annotation isn`t supposed to change what code does,
or so I thought:

#------------------------------------------------------------
class Borg:
 instances:dict = {}

def   new  (cls, *args, **kargs):
# look up subclass instance cache
if Borg. instances.get(cls) is None:
Borg. instances[cls] = object.  new  (cls)
return Borg. instances[cls]


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


class FailingSingleton(Borg):

def   init  (self):
print(self.  class  .  name  , `:`)
try:
self.already initialized:bool
print(`already initialized`)
return

except AttributeError:
print(`initializing`)

self.already initialized = True
self.special value = 42

s = WorkingSingleton()
print(s.special value)

s = FailingSingleton()
print(s.special value)

#------------------------------------------------------------

Notice how Working* and Failing differ in the type annotation
of self.already initialized only.

Output:

WorkingSingleton :
initializing
42

FailingSingleton :
already initialized<====================== Huh ?
Traceback (most recent call last):
  File
"/home/ncq/Projekte/gm/git/gnumed/gnumed/client/testing/test-singleton.py", line 48, in 
    print(s.special value)
          ^^^^^^^^^^^^^^^
AttributeError: `FailingSingleton` object has no attribute `special value`


Where`s the error in my thinking (or code) ?

Thanks,
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



   GoldED+ VK   │                                                 │   09:55:30    
                                                                                
В этой области больше нет сообщений.

Остаться здесь
Перейти к списку сообщений
Перейти к списку эх