Skip to main content

On Sale: GamesAssetsToolsTabletopComics
Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines
(+1)

Just so you know, with inheritance the subclass has the same functions as the base class. Which is to say, you don't need to call (from the weapon class):

Equipable.equip(self, target)

You can actually just call:

self.equip(target)

Calling it by class is normally only done if you are using multiple inheritance and accidentally gave two of the base classes conflicting functions with the same name, or you are for some reason calling it from outside the class and its subclasses.

By the way, when self is passed as an argument to a function it isn't actually calling itself, it is just passing itself as reference. Remember how I mentioned you could call a class function from outside a class? This is what makes that possible. Looking back at the above function, you might notice that there are two arguments in the first but only one in the second. When calling from outside, you need to provide the object, but by calling from the object (in this case 'self'), it implicitly passes itself as the first argument. So technically, both have the same two arguments.

That's why self is a must thing, as the object itself will always be the first argument when called normally. If you really want, there are ways to pass the object reference in other positions, but those are outside the scope of an itch.io comment and have no benefit other than novelty (and some downright masochistic downsides).

By the way, self is just a variable name. You could alter:

InventoryItem.__init__(self,name,...):

to:

InventoryItem.__init__(litterallyAnything,name,...)

but please never do that. It really hurts code readability, and you would also have to change

self.name = name

to

litterallyAnything.name = name

and etc.

(+1)

Let me add one caveat, when calling the __init__ function of the super class from the sub class, you should use the SuperClass.Function(self, args) format. I'm not super familiar with Python 2, but I think the reason is that at this point the object is not yet fully initialized so you can't call from it yet. I think there is a function called super() that gives access to the base class so you can call it without passing self, something like

# The following would be the first line of weapon class __init__
super().__init__(name,img,value,description,number,stat)

But you need the base class to inherit from object and the one time I tried coding with Python 2 I couldn't get it to work (I was using an unofficial interpreter, so you may get better results).