4
votes

Does the following have any practical use in Python?

>>> def a(n):
        print(n)
        return a

Or even:

>>> def a(n):
        print(n)
        return b
>>> def b(n):
        print(n+3)
        return a
1
You can call a like a(3)(4)(5), which will print 3 4 5 - Christian
I don't think it's useful. Whatever called the function must already have a reference to it. Maybe to shorten code somewhat, but that would be a very unusual way to write Python. - RemcoGerlich
@TimPietzcker no, it will not print something like that, but if you execute this in the interactive interpreter, the interpreter will attempt to print the return value of the call, which happens to be represented by a string like this. I guess you're aware of that distinction, but too many people are not, so please choose your words more carefully if you don't want this misunderstanding to spread even further... - l4mpi
@l4mpi If he is able to chain the calls, then the returned object is a function, unless he uses a custom object which implements __call__. So, when you print the expression/evaluate it in REPL, it will print as Tim has shown, I believe. - thefourtheye
@thefourtheye you didn't get my point. Of course the REPL will print it like this, but too many people are unaware of this distinction and think python itself prints the return values. My point is Tims comment is only valid if you execute the code in the interactive interpreter, and describes behaviour of the interactive interpreter, not of python as a language. - l4mpi

1 Answers

2
votes

This is common practice, maybe not so much with functions but widely used in OOP. Basically, whenever you're not using a getter (a method that returns properties of the object) or returning something specific, there is no cost to returning the object itself. But it allows to compress code as in

house = House()
exits = house.setDoors(2).setWindows(4).getNumberOfEmergencyExitsRequired()

While alternatively, you would have to write

house = House()
house.setDoors(2)
house.setWindows(4)
exits = house.getNumberOfEmergencyExistsRequired()

It's not the end of the world, but it allows to compress code without reducing readability, hence it is a nice thing.

To your examples

The first one is straight forward and similar, it allows compression of code. The second one is actually not something I personally would do, because

a(3)(5) == a(3); b(5)

In this simple example, there is no reason why it should behave like that and might be confusing.

Back to OOP

Anyhow, in OOP, of course you could imagine

class House(object):
    def addDoorByColor(self, doorColor):
        door = new Door()
        door.setColor(doorColor)
        self.door = door
        return self.door

Where then

house = House(); 
house.addDoorByColor('red').open()

would "open the door". This is probably not the best example for this scenario, but something I came along with right now just to show that there is potential use of returning "other objects". However, the last case would probably better be done by

door = new Door('red')
house.addDoor(door)
door.open()