python - How to get a reference to the current class from class body? -
I want to keep a dictionary of subclasses (all, non-instantly included) in the base class, so that I can instantiate one String them from I am doing this because CLSID
is sent through a web form, so I want to restrict the options set with subclasses (I eval ()
/ Globals ()
does not want to be classname). Class Base Class: CLSID = 'base' classes = {} def by_string (string): return class [str] () class fu (base class): CLSID = 'Foo' base class classes [CLSID] = Fu class Bar (Base Class): CLSID = 'Bar' Base Class. Classes [CLSID] = Bar
This does not clearly work, but is there anything like a I thought about using the So, hopefully I have explained my purpose, the question is How does that work? Or am I going to go completely wrong about this? @ classmethod
for the input? The idea is that this classmath will run only once because the class is read and registered with each class based on the class. Some of the following can do such things: (additional lines will also be saved in Foo
and bar)
CLSID = 'base' classes = {} @ classmathape def __init __ (CLS): base class. Classes [CLSID] = CLF_String: String: Return Class [ST] ()
__ subclasses __
and then < Filters ()
on CLSID
, but it only works for immediate subclasses
randomly bind to the base class: / P>
class auto registration (Type): def __new __ (MCS, name, base, D): auto = type .__ new__ (MCS, name, base, D) if in the "id" D: # Register only if the ID attribute directly in itself is. ID in self._by_id: Increase ValueError ("Duplicate ID:% r"% self.ID) self._by_id [self.ID] = Self-return Self-class base (object): __metaclass__ = id = "base" @ class I Def def_id (cls, id): Return cls._by_id [id] () Class A (Base): ID = "A" class B (Base): ID = "B" "Print Base FROM_ID (" A ") Print Base: Frame_ID ("b")
Or keeping unrelated concerns separate:
class IDFactory object): def __init __ (self) : Self._by_id = {} def register (self, cls): auto_by_id [cls.ID] = CLS or Ccl DIF __call __ (self, id, * args, ** kwds): return self_by_id [id] (* args, ** kwds) # factory instead = factory id = idfc @) registrar class base (object ), Instead of using the AADIID function: id = "base" @ Factory. Registrar class A (Base): ID = "A" @ Factory. Registry class B (Base): ID = "B" Print Factory ("A") Print Factory ("B")
You You may have picked up on what I liked, separated from the class hierarchy, you can easily expand and modify, such as by registering under two names (using the id feature only allows one) ):
class idfactory (object): def __init __ (self): self._by_id = {} def register (self, cls): self _by_id [cls.ID] = CLS return CLC DIF register_s (self, name): DEF COVER (CLS): self _by_id [name] = Cls return cls return wrapper # ... @ factory.register_as ("A") # does not require an ID and can then use # registry # for the factory, even Mix and match the factory too. Register_as ("B") # Imagination we have got rid of class A (object): #and a perfection that now rolls ID = "A"
you keep it Decoupled the example of the factory on the basis of "Decrypted":
class IDFactory: # ... class base (object): factory = IDFactory () @classmethod def register (CLS, subclass): If the subclass.ID factory in cls: Increase ValueError ("duplicate at id:% r"% subclass.id) cls.factory [subclass.ID] = subclass return sub-class @base.feter.register # Still completely decoded # (this is a feature of the base, but it can be changed # without modifying easily @ below one category) @base. Registrar # Optionally more coupled, but potentially desired category A (base): ID = "A"
Comments
Post a Comment