adding methods at runtime and lambda

Peter Otten __peter__ at web.de
Fri May 4 14:05:08 EDT 2007


Mike wrote:

> staticmethod makes the function available to the whole class according
> to the docs. What if I only want it to be available on a particular
> instance? Say I'm adding abilities to a character in a game and I want
> to give a particular character the ability to 'NukeEverybody'. I don't
> want all characters of that type to be able to wipe out the entire
> planet, just the particular character that got the powerup.

Static methods are for specialists, you don't need them. But then, your
initial post looked like you were just exploring the possibilities...

You can 

- have the Character.nuke_everybody() method check a self._can_nuke_eb flag
- subclass the Character class with a NukingChar subclass and make only one
  instance of that class
- add an instancemethod to one Character instance

The simpler the approach you take the smarter you are ;)

Peter



More information about the Python-list mailing list