2

I am getting the error as shown in the question, and I can't figure out why. Even when trying other Stack Overflow methods of fixing this it doesn't work.

class Item(object):
    def __init__(self, name, style, quantity):
        self.name = name
        self.style = style
        self.quantity = quantity
    
class Weapon(Item):
    def __init__(self, name, style, quantity=1):
        Item.__init__(name, style, quantity,)

Bow = Weapon(name = "Bow", style = "WRanged", quantity = 1)

The lines affected with the error codes:

Traceback (most recent call last):
  File "C:\Stuff\SG\Work\Inventory.py", line 33, in <module>
    Bow = Weapon(name = "Bow", style = "WRanged", quantity = 1)
  File "C:\Stuff\SG\Work\Inventory.py", line 12, in __init__
    Item.__init__(name, style, quantity,)
TypeError: __init__() missing 1 required positional argument: 'quantity'
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
aimcreeper
  • 23
  • 1
  • 1
  • 4
  • To help people answer your question, you'll need to be more specific about the error. Please [edit] your post to incorporate the exact errors you get from running your [mcve] (preferably using copy+paste to avoid transcription errors). In particular, don't omit the line numbers - they're often crucial. – Toby Speight Aug 30 '16 at 15:30

4 Answers4

7

Change

Item.__init__(name, style, quantity,)

for

super().__init__(name, style, quantity)
rafaelc
  • 57,686
  • 15
  • 58
  • 82
2

You're missing self in the Item._init__(). You can either:

  1. Add self:

     class Weapon(Item):
         def __init__(self, name, style, quantity=1):
             Item.__init__(self, name, style, quantity)
    
  2. Use super:

     class Weapon(Item):
         def __init__(self, name, style, quantity=1):
             super(Weapon, self).__init__(name, style, quantity)
    
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
2

Calling Item.__init__ directly means you need to pass self as the first argument. Simply doing Item.__init__(name, style, quantity) means it thinks that name is the Item instance (self) and style is the name, quantity is the style and quantity is missing.

So you can just specify self as the first argument:

Item.__init__(self,name, style, quantity)

Or since the __init__ is in no way different you can just not override it at all:

class Weapon(Item):
     #default to using Item.__init__, no problem here
##    def __init__(self, name, style, quantity = 1):
##        Item.__init__(name, style, quantity)

Although this means that quantity is now a required argument but I don't think that will be an issue.

Or as others have already said you can use super() to figure out what methods it should use.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
1

Simply use super for inheritance in Python (read here for more details):

class Weapon(Item):
     def __init__(self, name, style, quantity = 1):
         super(Weapon, self).__init__(name, style, quantity)
Community
  • 1
  • 1
Matheus Portela
  • 2,420
  • 1
  • 21
  • 32