type annotation vs working code

Chris Angelico rosuav at gmail.com
Sun Oct 1 18:21:42 EDT 2023


On Mon, 2 Oct 2023 at 09:10, Barry via Python-list
<python-list at python.org> wrote:
>
>
>
> > On 1 Oct 2023, at 19:36, Richard Damon via Python-list <python-list at 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


More information about the Python-list mailing list