Say I have a json file look like:
{ "foo": ["hi", "there"], "bar": ["nothing"] } I'd like to create an abstract base class (ABC), where the name of abstract methods are the keys of the json above, i.e.:
from abc import ABCMeta, abstractmethod class MyABC(metaclass=ABCMeta): @abstractmethod def foo(self): pass @abstractmethod def bar(self): pass The problem is the json file actually has lots of keys. I wonder if there's any way like:
import json with open("the_json.json") as f: the_json = json.load(f) class MyABC(metaclass=ABCMeta): # for k in the_json.keys(): # create abstract method k Thanks for the suggestions from the comments, but somehow it doesn't work as expected. Here is what I tried:
class MyABC(metaclass=ABCMeta): pass def f(self): pass setattr(MyABC, "foo", abstractmethod(f)) # I also tried # setattr(MyABC, "foo", abstractmethod(lambda self: ...)) # Try to define another class that inherits MyABC class MyClass(MyABC): pass c = MyClass() # Now this should trigger TypeError but it doesn't # I can even call c.foo() without getting any errors
func = decorator(function)is the same as@decorator def func(): pass. Usesetattrby callingabstractmethod(method)....instead ofpass....is calledEllipsis.