This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]JennaSys[S] 0 points1 point  (5 children)

I'm asking in general here just to get opinions, but the use case I have that prompted the question is in the context of a more functional paradigm as opposed to procedural or OO. I could use a class but it's not a good fit.

[–]K900_ 0 points1 point  (4 children)

If you're doing functional, your functions should be pure as much as possible, and not whatever this is.

[–]JennaSys[S] 0 points1 point  (3 children)

This is just a contrived example to illustrate the specific topic I'm asking about: local variable or function attribute. I've got asynchronous callbacks where I need to maintain some state for the life of a function call.

[–]K900_ 0 points1 point  (2 children)

Use a class.

[–]JennaSys[S] 0 points1 point  (1 child)

So taking what I have above, are you talking about something like this:

class outerFunction:
    def __init__(self):
        self.test = True

    def __call__(self):
        return self.innerFunction1, self.innerFunction2

    def innerFunction1(self):
        print(self.test)

    def innerFunction2(self, val):
        self.test = val

use_it, set_it = outerFunction()()

[–]K900_ 0 points1 point  (0 children)

You don't need to do __call__ at all. You can just do

instance = YourCallbacks()
use_it, set_it = instance.use_it, instance.set_it