Basically, I want to create a "template" type-object with staticmethods
and then create a new object that inherits from it but uses the parent's methods using functools.partial
where the arguments of the child are used. The child's arguments have the same name as the arguments for the methods of the parent. The resulting paradigm would be something like this:
class Naked:
@staticmethod
def echo1(foo):
return f"echo1: {foo}"
@staticmethod
def echo2(bar, bla):
return f"echo2: {bar}, {bla}"
class Clothed(Naked):
def __init__(self, *args, **kwargs):
for arg in args:
setattr(self, arg, arg)
for k,v in kwargs.items():
setattr(self, k, v)
a = Clothed(foo = "Hello", bar = "World")
a.echo1 == "echo1: Hello"
a.echo2("wsup?") == "echo2: World, wsup?"
Here is an attempt that doesn't work:
from inspect import signature
from functools import partial
class Meta(type):
def __init__(cls, name, bases, attrs):
funcs = (k for k,v in attrs.items() if callable(v) and k not in dir(type)) #this doesn't work...
for func in funcs:
args = signature(func).keys() #function argument names
newargs = {arg:getattr(self, arg, None) for arg in args} #create dictionary with val from instance, how is this possible, no self here?
attrs[func] = partial(func,newargs)
return type.__init__(cls, name, bases, attrs)
class Naked(metaclass=Meta):
@staticmethod
def echo1(foo):
return f"echo1: {foo}"
@staticmethod
def echo2(bar, bla):
return f"echo2: {bar}, {bla}"
class Clothed(Naked):
def __init__(self, *args, **kwargs):
for arg in args:
setattr(self, arg, arg)
for k,v in kwargs.items():
setattr(self, k, v)