1

I have a player class and a weapon class. The weapon class has a reload method that checks the inventory property of the player. The player class has a weapon property that would hold the current weapon object, from which the reload method would be called. Since the player can change weapons, I would like to instantiate the player object without a weapon object, but have access to weapon object intellisense in development. Is this possible?

In short, I'd like to be able to see the methods of my weapon class from within my player class, without an instance being created.

madmonkey
  • 83
  • 8
  • I think your player class should have a weapon property which inherited from weapon class. – TomCat Nov 15 '21 at 00:08
  • You might want to create an empty constructor method in their respective classes – Larry the Llama Nov 15 '21 at 00:08
  • 1
    If your concern with your current design is mostly regarding intellisense, you might want to direct your question mostly in that direction. That is, how can you get type hinting related information for a not-yet-assigned attribute. There may be a way to hint the attribute appropriately that won't require any changes to the runtime operation of your code. – Blckknght Nov 15 '21 at 00:17

2 Answers2

2

Why not use a dummy weapon object that the player holds when they're not holding a weapon? It can be an invisible weapon and lets you access whatever you want in this context.

If it'll mess with the unarmed state, you can make the player use unarmed attack animations if that's needed.

sad_pug
  • 33
  • 6
0

You could make use of __dict__, to get all attributes of a class:

class Player:
    def __init__(self, name):
        self.name = name
        self.weapon = None

    def weaponsAvailable(self):
        print([w for w, _ in Weapon.__dict__.items() if not w.startswith('__')])


class Weapon:
    def gun(self):
        print('gun')

    def knife(self):
        print('knife')


p = Player('Player1')
p.weaponsAvailable()

Out:

['gun', 'knife']
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47