2

In my package, I have various functions (fun) that can interact with or modify stuff (for simplicity I use string fragments here), and return it. I now want to create a class that can collect what the functions return (Collector). I want those functions to become methods of the Collector class so that they can be called from its namespace - not directly though, but through a subclass which I call run.

So, when using collector.run.fun() I would like Collector to catch the output of fun so that it is available (in Collector's namespace) for further processing (e.g. by running more functions).

How do I dynamically add functions to a class and retain what they return in the class?

Here is some code. It's wrong, obviously - setattr will just attach fun to Collector.run...I don't know how to proceed though:

## package structure
## /my_pgk/module.py
## /my_pgk/sub/submodule.py

## many more like this in submodule.py:
def fun(add="default", **kwargs):
    if "base" in kwargs:
        add = kwargs.get("base") + " " + add 
    if "more" in kwargs:
        add = add + " " + kwargs.get("more")
    return add


## in module.py:
from sub import submodule

class Collector(object):
    def __init__(self, base, **kwargs):

        self.base = base
        if "more" in kwargs and not kwargs.get("more") == "":
            self.more = kwargs.get("more")

        class run(object):
            def __init__ (self):
                        
                ## dyanmically add class methods (this is wrong)
                for key, value in submodule.__dict__.items(): 
                    if callable(value) and not key.startswith('_'):    
                        setattr(self, key, value)                         
            
        self.run = run()     
        
        ## ENTER SOME PYTHON MAGIC HERE THAT ALLOWS ME TO RUN FUNCTIONS AS 
        ## CLASS METHODS AND CATCH THEIR OUTPUT FOR FURTHER PROCESSING:
            
        ## self.add = ...SOMEHOW CATCH METHOD OUTPUT...
        ## self.final = ", and ".join([self.base, self.add, self.more])
                
        
    def out(self):
        print(self.final)
    

## in practice:
col = Collector(base="(1) this is the start", 
          more="(3) even more is provided")
col.run.fun("(2) this is what I add")

col.out()
## should give 
# (1) this is the start, and (2) this is what I add, and (3) even more is provided
mluerig
  • 638
  • 10
  • 25
  • you could always just pass the class object to the function. this is internally what python does (which is why you need ```self``` at the start of member functions) – an inconspicuous semicolon Mar 23 '21 at 14:37
  • You could set `run = object()` and then the [2nd answer here to add the functions as methods](https://stackoverflow.com/a/28060251/1431750). Or put it in the `run.__init__` of you prefer but then it repeats for very instance of your class. Remember, the class body can also straight-up include executable code which will be evaluated at declaration time. So `run = object()` and then `setattr(run, func_name, func_obj)` or any of the alternatives from the linked answer. You'll need to add them as `staticmethod`'s since the submodule funcs have no `self` param. Hint: use the descriptor method. – aneroid Mar 23 '21 at 15:04
  • @reece that's what I had before, but it requires tons of boilerplate inside every function because the real object and function can collect and generate many different types, which I would like to deal with centrally – mluerig Mar 23 '21 at 15:52
  • @aneroid when you say "descriptor method", are you referring to the first method that is suggested in the linked answer? you also suggested using `setattr`...I'm confused – mluerig Mar 23 '21 at 17:30
  • realizing that this is about staticmethods I found [this](https://stackoverflow.com/questions/14645353/dynamically-added-static-methods-to-a-python-class) question. so I tried `run=object()` and `setattr(run, key, staticmethod(value))`, but I get `AttributeError: 'object' object has no attribute 'fun'` – mluerig Mar 23 '21 at 17:56
  • @mluerig Sorry, I mean to point to `type('', (), {})()`, [like in this answer](https://stackoverflow.com/a/24448351/1431750), for arbitrary attributes being added to on object. You could also try using [`SimpleNamespace`](https://docs.python.org/library/types.html#types.SimpleNamespace) like [in this answer](https://stackoverflow.com/a/34378560/1431750) but I haven't tried it with methods, should work. – aneroid Mar 23 '21 at 18:25
  • @mluerig Yes, I was referring to the first method in the 2nd answer, the one titled "Method nought (0) - use the descriptor method, `__get__`". – aneroid Mar 23 '21 at 18:27
  • ok using `type...` works, but I don't know how `__get__` would work, since I need to create the functions dynamically. would you mind creating an example to demonstrate all these suggestions together? I fail to see how this will allow me to "catch" the output of `fun`... – mluerig Mar 24 '21 at 09:41

0 Answers0