+ 1
Whatâs wrong with my code?
Hi again everyone, I really canât figure out whatâs wrong with this simple code. Can anyone help? Thank you, sorry if itâs dumb. https://code.sololearn.com/c58wcZSc6J5V/?ref=app
5 Answers
+ 2
I tried to tell, the correct way to create class with inheritence in your last question..
Look it again...
Your class declaration for child class is in wrong way,..
https://www.sololearn.com/Discuss/3046429/?ref=app
+ 1
So is the constructor for the child class required as well as super constructor? Thank you so much for your help.
+ 1
I want to have the parameters declared in the child class before the instance so they don't have to be declared with each instance. I am trying to create a "Pieces" class that has the basic parameters and a "GreenPawns" class which inherets and fills in the parameters, and then an instance of each pawn. I hope this isn't too much, but thank you for trying to help.
+ 1
Sorry! I think I figured it out now. This is what I wanted:
class Pieces:
def __init__ (self, color, letter, start_range, move, capture):
self.color = color
self.letter = letter
self.start_range = start_range
self.move = move
self.capture = capture
class GreenPawn(Pieces):
color = 'green'
letter = 'P'
start_range = [8,16]
move = [8,16]
capture = [7,8,14,16]
pass
+ 1
#May be you are trying like this :
class Pieces:
def __init__(self, color, letter, start_range, move, capture):
self.color = color
self.letter = letter
self.start_range = start_range
self.move = move
self.capture = capture
class GreenPawn(Pieces):
def __init__(self,color='green', letter='P', start_range=[8,16], move=[8,16], capture=[7,9]):
super().__init__(color,letter,start_range,move,capture)
self.color = color
self.letter = letter
self.start_range = start_range
self.move = move
self.capture = capture
obj = GreenPawn() #none passing
print(obj.color)
#Hope it helps to understand it..
Base class is called in the subclass constructor by super() method. And it have **keyword arguments so you can pass only required parameters in the object. I think, You can also, reduce initializations in the child class if any unnecessary redundents.. You can try if you want..