I try to create classes with quite extensive methods defined in multiple modules. I need some wrappers to ensure compatibility with other classes and cannot change my modules greatly. Here is an attempt of a minimal example:
Let my (many) modules be something like:
"""moduleA.py"""
def print_something(text):
print(f"Here is some output from module A: {text}")
and just a second for the example:
"""moduleB.py"""
def print_something(text):
print(f"Here is some output from module B: {text}")
If I build a dynamic class definition around many of such modules, the assignment of my method does not work as hoped:
import os
import re
from importlib import import_module
# create class for every pyd-module in current working directory
cwd = os.getcwd()
for fileName in os.listdir(cwd):
if re.search('module.+\.py', fileName):
moduleName = re.split('\.', fileName)[0]
module = import_module(moduleName)
# wrapper does something needed
def print_wrapper(self, text):
module.print_something(text.upper())
className = 'ClassM'+moduleName[1:]
dynamicClass = type(className, (object, ), {
"print_something": print_wrapper})
# register dynamic class for use
globals()[dynamicClass.__name__] = dynamicClass
instanceA = ClassModuleA()
instanceA.print_something("testA")
instanceB = ClassModuleB()
instanceB.print_something("testB")
output (only from module B and not from module A):
Here is some output from module B: TESTA Here is some output from module B: TESTB
I tried to use copy operations, but they do not work for builtin_function_or_method(s). (see: How to get builtin_function_or_method objects copied in ram without wrapping? and How can I make a deepcopy of a function in Python?)
Is there a way around this problem?