I am trying to write a function drag which can be called as drag('Object1').to('Object2'). The code below works as I want, but it seems using a class simply as a container may be unnecessary:
def drag(source):
class Container:
def to(self, target):
print 'Dragging ' + source + ' to ' + target
return Container()
Can the above code be simplified?
I think you can use a class that has a source and when
tois called takes a target and prints the message. You can call the classdrag:Or you can use a
Containerclass as in your code and havedragsimply be a function that calls the initializer:I will point out that this function doesn’t really do any thing useful, and attempting to write a useful class/function like this will make your code less clear without adding anything.