0

I'm trying to pass a method to class Fighter. The method requires me to pass the instance of a the fighter class where I would normally just be able to use self. Is there any way to use self in a function that I'm passing to a class.

The constructor looks like this def melee_attack(fighter, target) and I would rather it look like this def melee_attack(self, target): so that when I call it I can just call fighter.attack(target) instead of fighter.attack(fighter, target)

Sig
  • 91
  • 2
  • 12
  • Yes?! What exactly is the problem with declaring the method like that? This needs more information. – deceze Mar 31 '20 at 07:39
  • I don't want to pass the entire fighter object into the method. It'd be easier to just pass the target if that's possible. – Sig Mar 31 '20 at 07:42
  • Still not clear enough to be answerable. If you declare `class Fighter: def melee_attack(self, target): ...`, it works exactly as you want it. – deceze Mar 31 '20 at 07:44
  • I'm not declaring melee_attack in fighter. I'm passing it in the constructor for Fighter, so that `self.attack=melee_attack`. I want to be able to use different attack type for different fighters. – Sig Mar 31 '20 at 07:47
  • OK, so probably https://stackoverflow.com/a/2982/476 then. – deceze Mar 31 '20 at 07:53
  • I'm not sure how to use this. – Sig Mar 31 '20 at 08:03
  • 1
    Like this: https://repl.it/repls/HelpfulWobblyAudacity – deceze Mar 31 '20 at 08:10
  • This works perfectly, thank you. – Sig Mar 31 '20 at 21:49

0 Answers0