Is it pythonic for a function to return chained values / set variables on itself?

python

Solution

You're not "returning chained values", you're creating a function which returns itself, after setting variables on itself.

The problem with this is that if you reinvoke the function (assuming it isn't just a constant function as shown in your example) is that every single appearance of the function (and understand that `r` is the same as `f` in your code) will have those values change. You'll have this problem whether or not your programme uses multiple threads.

The normal way to return multiple values is simply to return a tuple, which can be the source of a destructuring (sequence) assignment. Alternatively, if you want to manage a bunch of variables together, you would use an object. That's what they're for.

Problem

Is it pythonic to return multiple values from a function in this way? ``` def f(): f.x = 1 f.y = 2 return f r = f() print r.x,r.y 1 2 ```

Original source