InsideDarkWeb.com

How to call a child class with no parameter in contructor in python?

I have a Multiple Parent class and the superparent class is the

class Item:
    def __init__(self, name):
        self.name = name
    
    def print_name(self):
        print("The item name is " + self.name)

and the base class is:

class Gadget(Item):
    def __init__(self, name,
       version):
        self.name = name
        self.version = version
 
    def print_attribute(self):
       pass
       #do for attribute

The child of the base class is:

class Mobile_Phone(Gadget):
    def __init__():
        pass

so when i instatiate the child class

item = Mobile_Phone("Iphone", "iOS")
item.print_name()

it gives me an error the contuctor has 0 positional argument but 3 were given

Stack Overflow Asked by mather on November 18, 2021

2 Answers

2 Answers

If your child class initializer doesn't do anything with the arguments you're passing, then they're lost. In the model you've outlined, you could simply omit the initializer in the child class to get what you want.

class MobilePhone(Gadget):  # PEP8 calls for CamelCase here
    pass

In this case, the initializer inherited from Gadget is setting self.name which the inherited Item.print_name references.

Answered by Sam Morgan on November 18, 2021

You need to understand the concept of OOPs and if you send arguments during object construction then your constructor should have parameters to hold the arguments.

item.py

class Item:
    def __init__(self, name):
        self.name = name
    
    def print_name(self):
        print("The item name is " + self.name)

Gadget.py

from item import Item

class Gadget(Item):
    def __init__(self, name, version):
        Item.__init__(self, name)
        self.version = version
 
    def print_attribute(self):
       print(self.name)
       print(self.version)

Mobile_Phone.py

from Gadget import Gadget

class Mobile_Phone(Gadget):
    def __init__(self, name, version):
        Gadget.__init__(self, name, version)


item = Mobile_Phone("Iphone", "iOS")
item.print_name()

Output:

The item name is Iphone

Answered by bigbounty on November 18, 2021

Add your own answers!

Related Questions

Update Subform based on Form Combo Box

0  Asked on December 10, 2020 by hazat-bangurah

   

Determine if __getattr__ is method or attribute call

3  Asked on December 10, 2020 by robin-orheden

   

How to implement category axis in matplotlib?

0  Asked on December 10, 2020 by fgg1991

   

Pre-fill the checkbox with url

2  Asked on December 10, 2020 by sevenine

       

Shell=false generates OsError

1  Asked on December 10, 2020 by azza-abidi

       

LINQ query orderBy a subquery

2  Asked on December 9, 2020 by golfbravo

       

Avast blocks pip and Pyinstaller

1  Asked on December 9, 2020 by dripis

         

Trying to grasp callbacks javascript

2  Asked on December 9, 2020 by justaordinarymonkey

 

Typescript get distinct values from an object

3  Asked on December 9, 2020 by nikhila-reddy-c

     

Strip n from list created from txt file

1  Asked on December 9, 2020 by nataku62

       

Ask a Question

Get help from others!

© 2021 InsideDarkWeb.com. All rights reserved.