+ 1

Inheritance question?

Hi everyone! My question to inheritance in Python. I am trying to create a superclass called pieces which the class GreenPawns inherits from. I then want to create a list of instances of GreenPawns, but am running into trouble. I get the error message: 'TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases' I am not sure why. I am trying to understand OOP. I attached the code I wrote in the comments. Can anyone tell me what I am doing wrong? Thank you.

14th Jun 2022, 1:42 PM
madeline
madeline - avatar
5 Respuestas
+ 1
I'm not too familiar with Python's way with inheritance, but looking at line 10, I can't help noticing how can you pass the class constructor's arguments on that line. Shouldn't those values be passed and read in inside the class' constructor?
14th Jun 2022, 3:11 PM
Ipang
+ 2
Am also not much familier with python, but as @ipang said, inheritence syntax is like : class A: #code class B(A): #code And ' __init__( ) ' method is a contructor, for a class. So add parameters list to constuctor __init__( ) and pass parameters in object calling... edit: corrected sample greenpawns =[] class Pieces(): def __init__ (self, color, letter, start_range): self.color = color self.letter = letter self.start_range = start_range class GreenPawns(Pieces): def __init__(self, *args): super().__init__(*args) greenpawns.append(GreenPawns('green', 'P', range(8,16))) print(greenpawns[0].color) hope it helps....
14th Jun 2022, 5:16 PM
Jayakrishna 🇮🇳
+ 2
Hi Jayakrishna🇮🇳, very nice, clean solution and exactly what I was looking for! Thanks! :)
14th Jun 2022, 10:39 PM
madeline
madeline - avatar
14th Jun 2022, 1:43 PM
madeline
madeline - avatar
+ 1
lpang, Thank you for your help, I remember you helping me before. I see what you mean, where the parameters should be declared inside the GreenPawns constructor. That helps me create the new class from Pieces, which is what I was trying to do. As far as resolving the issue, I think I still just need to experiment more. I am self-taught, and trying to create a chess game. Anyway, thank you for helping me!
14th Jun 2022, 3:46 PM
madeline
madeline - avatar