2

I try to make a Toy class here

class Toy:

  def __init__(self,name,ID,price,age):


    self.__ToyName = name  
    self.__ToyID = ID
    self.__Price = price
    self.__MinimumAge = int(age)
##some methods here

And when i try to make a sub-class computer game, i need to make 7 arguments(with 5 from the Toy class)to instantiating the computer game class, and it shows "too many arguements (7/5)"

class ComputerGame(Toy): 
  def __init__(self,name,ID,price,age,catogory,console):

    Toy.__init__(self,name,ID,price,age)
    self.__Catogory = catogory
    self.__Console = console

What should I do to with this situation?

Jacob.L
  • 21
  • 1
  • 3
  • 1
    Possible duplicate of [How to instantiate a class in python](https://stackoverflow.com/questions/396856/how-to-instantiate-a-class-in-python) – Ken Y-N Jan 26 '18 at 07:42
  • `self.toy = Toy(name,ID,price,age)` or similar is what you want. Check the dup for more details. – Ken Y-N Jan 26 '18 at 07:43
  • 1
    I don't know what you did, because I see no problem in you code, and my python doesn't either... – Camion Jan 26 '18 at 07:52
  • I'd write `super().__init__(name, ID, price, age)` but be careful, `super` does not work like in other languages. Your code is correct, tho. You should just do `ComputerGame(name, ID, price, age, category, console)` – Jose A. García Jan 26 '18 at 08:01
  • Also, unless you really know what you're doing and why, don't use class attribute names starting with a double underscore. See (https://stackoverflow.com/questions/1301346/what-is-the-meaning-of-a-single-and-a-double-underscore-before-an-object-name) – Thierry Lathuille Jan 26 '18 at 08:06

1 Answers1

2

you need learn more about super

class ComputerGame(Toy): 
  def __init__(self, name, ID, price, age, catogory, console):
    super(ComputerGame, self).__init__(name, ID, price, age)
    self.__Catogory = catogory
    self.__Console = console
mouse
  • 21
  • 5
  • There are information about [super](https://docs.python.org/3/library/functions.html?highlight=super#super) – mouse Jan 26 '18 at 07:58
  • Thanks! Does it mean if I got a sub class with multiple variables I will have to use the `super` method? And how do I deal with class that itself has a very long list of variables? – Jacob.L Jan 26 '18 at 07:58
  • There is no problem that you give a very long list of variables. However I have a little suggestion about parameters: 1. Whether need split to more class, 2. Maybe give some default parameter? 3. Use keywords parameters? – mouse Jan 26 '18 at 08:04
  • A blog about [super](https://rhettinger.wordpress.com/2011/05/26/super-considered-super/) – mouse Jan 26 '18 at 08:13