On Mon, 2 Oct 2023 at 09:10, Barry via Python-list
<python-list@python.org> wrote:
>
>
>
> > On 1 Oct 2023, at 19:36, Richard Damon via Python-list 
> > <python-list@python.org> 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 of 
> > False.
> I would use a class variable not an instance variable.
>
> class OnlyOne:
>        sole_instance = None
>        def __init__(self):
>               assert OnlyOne.sole_instance is None
>               OnlyOne.sole_instance = self
>

Agreed, except that this should be an if-raise rather than an assert.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to