3

I have a class which maintains a list of functions. These functions are just objects sitting in a queue and every so often the class pops one off and executes it. However, there are times when I would like to print out this list, and I'm imagining code as follows:

for function in self.control_queue:
    print function.summarize()
    if function.ready():
        function()

In other words, I would like to call methods called summarize() and ready(), that I want to define somewhere, on these function objects. Also, I would like to be able to toss anonymous functions on this queue - i.e., generate everything dynamically.

6
  • I'm not a python expert, but wouldn't you use a class to encapsulate the Summarize and Ready methods? Commented Jan 13, 2014 at 17:30
  • 2
    Are you looking for something like PEP 232? Commented Jan 13, 2014 at 17:31
  • Would that mean that functions are not first class objects, then? Commented Jan 13, 2014 at 17:31
  • I'll have to take some time to read over PEP 232. Commented Jan 13, 2014 at 17:34
  • You can just add new functions as attributes to a function object. I don't know that there's a sensible way to make them actual bound instance methods, though. Commented Jan 13, 2014 at 17:38

3 Answers 3

1

you can make it a class and define __call__

class MyClass():
    def summarize(self):
        #summarize stuff
        pass

    def ready(self):
        #ready stuff
        pass

    def _call__(self):
        #put the code here, for when you call myClass()
        pass

How you run it:

function = MyClass()
print function.summarize()
if function.ready():
    function()
Sign up to request clarification or add additional context in comments.

Comments

1

You have a couple possible approaches.

You could add the definitions to functions.

def foo():
    pass
# later..
foo.summarize = lambda: "To pair with bar"
foo.ready = lambda: True

You could create class objects to wrap the function operation.

class Func():
    def summarize(self):
        return "Function!"

    def ready(self):
        return self.ready

    def __call__(self):
        # Act as a function

Or you can have a function which checks the function label for these capabilities.

def summarize_func(func):
    return func.__name__ # Or branch here on specific names/attributes

def ready_func(func):
    return True # Or branch on names/attributes

Finally to accommodate anonymous functions you can check for prescience of these attributes and return optimistically if the attributes are absent. Then you can combine above approaches with something that will work on any function.

def summarize_func(func):
    if hasattr(func, summarize):
        return func.summarize()
    else:
        # Note this will just be '<lambda>' for anonymous funcs
        return func.__name__

def ready_func(func):
    if hasattr(func, ready):
        return func.ready()
    else:
        return True

Comments

0

One option is to implement function as a class instance:

class Function(object):
     def summarize(self): pass # some relevant code here
     def __call__(self): pass  #    and there

and use it later with

function = Function()

With __call__ magic method implemented, this function becomes a callable object.

For sure, you can assign attributes to functions, but it is rather obscure and conterintuitive:

>>> def summ(a): return sum(a)
...
>>> def function(a): return a
...
>>> function.sum=summ
>>> function.sum([1,2,3])
6                  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.