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
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()
alikea(3)(4)(5), which will print3 4 5- Christianprintsomething 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__call__. So, when you print the expression/evaluate it in REPL, it will print as Tim has shown, I believe. - thefourtheye