Skip to content

Instantly share code, notes, and snippets.

@rrmerugu
Created April 1, 2020 14:50
Show Gist options
  • Save rrmerugu/fd24f447bb7e23d51c9be843dbe20219 to your computer and use it in GitHub Desktop.
Save rrmerugu/fd24f447bb7e23d51c9be843dbe20219 to your computer and use it in GitHub Desktop.
register classes to a dictionary
registry = {}
def register_microservice(cls):
name = cls.__name__
force_bound = False
if '__init__' in cls.__dict__:
cls.__init__.func_globals[name] = cls
force_bound = True
try:
registry[name] = cls()
finally:
if force_bound:
del cls.__init__.func_globals[name]
return cls
@register_microservice
class HelloWorld:
pass
print(registry)
# {'HelloWorld': <__main__.HelloWorld object at 0x107ddfbd0>}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment