-2

SO has lots of posts on unbound vs. bound methods.

I understand the definition. Do unbound methods have any utility unique to this category? If so, could someone provide an example? I am not asking for a best application, simply a unique application.

Community
  • 1
  • 1
sunny
  • 3,853
  • 5
  • 32
  • 62

1 Answers1

0

The most obvious use case that comes to mind is for a control system. Imagine you have many players in a game where they can send you various commands across the network.. you might want to create a dict like:

player_commands = {
  'move'  : Player.move
  'attack': Player.attack
  ...
}

Then you can read the string and call the appropriate method with any Player object

player_commands[cmd](theplayer)

And it saves you having to have a big if/else chain like

if cmd == 'move':
      theplayer.move()
   elif cmd == 'attack':
      ...

Also since it's a dictionary you can update/edit it dynamically.

Chad S.
  • 6,252
  • 15
  • 25