Re: type annotation vs working code

2023-10-04 Thread Karsten Hilbert via Python-list
Am Wed, Oct 04, 2023 at 05:25:04PM +1300 schrieb dn via Python-list: > The first question when dealing with the Singleton Pattern is what to do when > more than > one instantiation is attempted: > > - silently return the first instance This, in my case. > and so, returning to the matter of

Re: type annotation vs working code

2023-10-04 Thread dn via Python-list
On 04/10/2023 19.41, Chris Angelico via Python-list wrote: On Wed, 4 Oct 2023 at 15:27, dn via Python-list wrote: - should the class have been called either; class SomethingSingleton(): or a Singleton() class defined, which is then sub-classed, ie class Something( Singleton ):

Re: type annotation vs working code

2023-10-04 Thread Chris Angelico via Python-list
On Wed, 4 Oct 2023 at 17:47, Greg Ewing via Python-list wrote: > > On 4/10/23 5:25 pm, dn wrote: > > The first question when dealing with the Singleton Pattern is what to do > > when more than one instantiation is attempted > > My preferred way of handling singletons is not to expose the class >

Re: type annotation vs working code

2023-10-04 Thread Greg Ewing via Python-list
On 4/10/23 5:25 pm, dn wrote: The first question when dealing with the Singleton Pattern is what to do when more than one instantiation is attempted My preferred way of handling singletons is not to expose the class itself, but a function that creates an instance the first time it's called,

Re: type annotation vs working code

2023-10-04 Thread Chris Angelico via Python-list
On Wed, 4 Oct 2023 at 15:27, dn via Python-list wrote: > - should the class have been called either; > > class SomethingSingleton(): > > or a Singleton() class defined, which is then sub-classed, ie > > class Something( Singleton ): > > in order to better communicate the coder's intent

Re: type annotation vs working code

2023-10-03 Thread dn via Python-list
On 02/10/2023 00.57, Karsten Hilbert via Python-list wrote: Sorry for having conflated the core of the matter with all the Borg shenanigans, that's where I found the problem in my real code, so there :-) The first question when dealing with the Singleton Pattern is what to do when more than

Re: type annotation vs working code

2023-10-01 Thread Chris Angelico via Python-list
On Mon, 2 Oct 2023 at 09:10, Barry via Python-list wrote: > > > > > On 1 Oct 2023, at 19:36, Richard Damon via Python-list > > wrote: > > > > Perhaps a better method would be rather than just using the name and > > catching the exception, use a real already_initialized flag (set to True > >

Re: type annotation vs working code

2023-10-01 Thread Barry via Python-list
> On 1 Oct 2023, at 19:36, Richard Damon via Python-list > wrote: > > Perhaps a better method would be rather than just using the name and catching > the exception, use a real already_initialized flag (set to True when you > initialize), and look it up with getattr() with a default value

Re: type annotation vs working code

2023-10-01 Thread Richard Damon via Python-list
My view of the issue is that the "trick" of "evaluating" a name to see if the object has been initialized is just a tad on the "tricky" side, and the annotation/value is really incorrect. The name at the point you are annotating it, isn't really a "bool" because a bool will always have either

Re: type annotation vs working code

2023-10-01 Thread Chris Angelico via Python-list
On Sun, 1 Oct 2023 at 22:58, Karsten Hilbert via Python-list wrote: > > Sorry for having conflated the core of the matter with all > the Borg shenanigans, that's where I found the problem in my > real code, so there :-) > > Consider this: > > #

Re: type annotation vs working code

2023-10-01 Thread Karsten Hilbert via Python-list
Sorry for having conflated the core of the matter with all the Borg shenanigans, that's where I found the problem in my real code, so there :-) Consider this: # class Surprise: def __init__(self, with_type_annotation=False):

Re: type annotation vs working code

2023-09-30 Thread dn via Python-list
On 01/10/2023 11.25, Karsten Hilbert via Python-list wrote: 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:

Re: type annotation vs working code

2023-09-30 Thread Karsten Hilbert via Python-list
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

Re: type annotation vs working code

2023-09-30 Thread dn via Python-list
On 01/10/2023 08.00, Karsten Hilbert via Python-list wrote: A type annotation isn't supposed to change what code does, or so I thought: # class Borg: _instances:dict = {} def __new__(cls, *args, **kargs):

Re: type annotation vs working code

2023-09-30 Thread Mats Wichmann via Python-list
On 9/30/23 13:00, Karsten Hilbert via Python-list wrote: A type annotation isn't supposed to change what code does, or so I thought: # class Borg: _instances:dict = {} def __new__(cls, *args, **kargs):

type annotation vs working code

2023-09-30 Thread Karsten Hilbert via Python-list
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