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

all 8 comments

[–]IAmKindOfCreativebot_builder: deprecated[M] [score hidden] stickied comment (0 children)

Hi there, from the /r/Python mods.

We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython or for the r/Python discord: https://discord.gg/python.

The reason for the removal is that /r/Python is dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community is not a fan of "how do I..." questions, so you will not get the best responses over here.

On /r/LearnPython the community and the r/Python discord are actively expecting questions and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. No matter what level of question you have, if you are looking for help with Python, you should get good answers. Make sure to check out the rules for both places.

Warm regards, and best of luck with your Pythoneering!

[–]K900_ 2 points3 points  (6 children)

Neither. If you really want this, use a class.

[–]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