-3

I want my Board class to inherit from my Cell class. My Cell class accepts 3 parameters in it's __init__ function but I do not want that in the __init__ function in my Board class. Is it possible to do something like this?

class Board(Cell):
    def __init__(self):
        super().__init__(x, y, w)
        self.x = x
        self.y = y
        self.w = w 

instead of doing this:

class Board(Cell):
    def __init__(self, x, y, w):
        super().__init__(x, y, w)
        self.x = x
        self.y = y
        self.w = w

My goal is to make a 2d array called self.board inside of Board class.
self.board = [[Cell(i * w, j * w, w) for i in range(3)] for j in range(3)]. Because of this I do not want to inherit the arguments from the cell class, because when I call the Board class I want to create it without any arguments.

Michael
  • 367
  • 2
  • 10
Sogokong
  • 7
  • 3
  • 4
    Yes, but where would you get the `x`, `y`, and `w` if not from the arguments? – zvone Jan 26 '19 at 11:19
  • 5
    And either way, after calling `super().__init__(x, y, w)`, you don't need `self.x = x` etc. – zvone Jan 26 '19 at 11:20
  • How do I pass the arguments from super class to sub class without doing it? – Sogokong Jan 26 '19 at 11:22
  • 2
    You don't pass arguments from super class to sub class in any of this. I believe you have a rather wrong idea about how this works. What are you actually trying to accomplish? – zvone Jan 26 '19 at 11:26
  • 3
    Further, a board should probably not *be* a cell, that makes little logical sense. A board should probably be *composed of* many cells (`self.cells = []`). – deceze Jan 26 '19 at 11:31
  • The thin i try to d is to make a 2d list of cells calld board `self.board = [[Cell(i * w, j * w, w) for i in range(3)] for j in range(3)]` @zvone. Do I need to call super when i do not want to use x, y w in Board class? – Sogokong Jan 26 '19 at 11:57
  • This has had a few downvotes but I think it's a nice example of trying to do inheritance where composition might be better. We're supposed to be here to help, especially for new contributors, so actually I think it's reasonably valid. – Michael Jan 26 '19 at 12:48
  • 2
    @Michael The question right now simply does not make sense - he wants to inherit but does not want to inherit, and he does not want to pass on arguments but has to do so. Composition may or may not be adequate, it is impossible to tell. – MisterMiyagi Jan 26 '19 at 12:56
  • 1
    @Sogokong *Why* do you want to inherit Board from Cell? – MisterMiyagi Jan 26 '19 at 13:04
  • 1
    @mistermiyagi I think it's mainly an English / second language problem. I've put in an edit to try to help with that - if you can see it please review and accept. I think this is a fairly standard beginning problem - someone has been taught about object inheritance but the teacher didn't tell them about composition. Now they want to achieve something (a Board object that uses the features of a Cell object) but since they only know inheritance they are having difficulties, even with explaining their problem. (It's very difficult to say "I don't now what composition is, please tell me?") – Michael Jan 27 '19 at 10:41

1 Answers1

2

It almost certainly doesn't make sense in your case but there's nothing stopping you from providing a different init and reusing the superclass init in that. You can just set fixed variables before calling the super class. For example:

class Board(Cell):
    def __init__(self):
        x = 8
        y = 8
        w = 64
        super().__init__(x, y, w)
        self.x = x
        self.y = y
        self.w = w 

However in your case you want to contain multiple cells in one board. That means that composition, where one object uses another object rather than inheriting from it is better. Instead you could include a set of cell object in your board and use them appropriately. Here's an example where cells can be activated and we provide a method on the board

class Board:
    def __init__(self):
        self.board = [[Cell(i * w, j * w, w) for i in range(3)] 
                       for j in range(3)]

    def activate_cell(x,y):
        self.board[x][y].activate()


b=Board()
#make the middle cell active
b.activate(1,1)

the fact that your two objects have different interfaces is normally a good hint that they aren't really the same thing and so inheritance isn't as appropriate as composition.

For more about this see https://en.wikipedia.org/wiki/Composition_over_inheritance which has a nice discussion. There's also a stack overflow question where the first answer gives a good explanation of the benefits of composition and when to choose inheritance instead - Prefer composition over inheritance? .

Michael
  • 367
  • 2
  • 10
  • 1
    @Sogokong if this solves your problem could you please accept it as an answer. If it doesn't answer your question could you please explain what is missing or what is unclear to you. - All the best. M. – Michael Jan 27 '19 at 10:24