|
From: | John Snow |
Subject: | Re: [PATCH 08/12] qapi/schema: make QAPISourceInfo mandatory |
Date: | Fri, 18 Dec 2020 16:30:31 -0500 |
User-agent: | Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.3.1 |
On 12/18/20 3:57 PM, Markus Armbruster wrote:
John Snow <jsnow@redhat.com> writes:On 12/18/20 12:24 AM, Markus Armbruster wrote:I could conceivably use source line information and stuff, to be needlessly fancy about it. Nah. I just think singleton patterns are kind of weird to implement in Python, so I didn't.Stupidest singleton that could possibly work: in __init__, self.singleton = ...Yeah, you can make a class variable that has a builtin singleton, then make the class method return that class variable. Feels fancier than my laziness permits. I just put it back to using one copy per definition.Why have a class method around the attribute? Just use the stoopid attribute already ;-P
Something has to initialize it: ``` class Blah: magic = Blah() def __init__(self): pass ``` Won't work; Blah isn't defined yet. a classmethod works though: ``` class Blah: magic = None def __init__(self) -> None: pass @classmethod def make(cls) -> 'Blah': if cls.magic is None: cls.magic = cls() return cls.magic ```
[Prev in Thread] | Current Thread | [Next in Thread] |