Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate a subclass, instead, when calling the creation of a class, depending on some logic
    primarykey
    data
    text
    <p><em>The question that I posted initially was lacking so here is an explanation that I hope will satisfy everyone and cancel some down votes that I received on it.</em></p> <p>I would like to have an intelligence inside a class, that with the creation of an instance, it is decided that the instance would be of a different class, which is one of the subclasses of this class, according to some logic.</p> <p>More specifically, I am making a <a href="https://en.wikipedia.org/wiki/Magic_square" rel="nofollow">Magic Square</a> solver as a learning exercise and:</p> <ul> <li>I want to have a <code>MagicSquare</code> class that will contain the logic of a MagicSquare.</li> <li>I want to have <code>OddMagicSquare</code> and <code>EvenMagicSquare</code> subclasses of that class that will contain the logics of solving these two types of Magic Squares.</li> <li>I want to be able to call the creation of a <code>MagicSquare</code>, providing it's size, <code>n</code>, and have the intelligence within <code>MagicSquare</code> determine which subclass to create an instance of, instead of the generic, top, class <code>MagicSquare</code>.</li> </ul> <p>I understand that the intelligence to determine which subclass (<code>OddMagicSquare</code>/<code>EvenMagicSquare</code>) to create an instance of <em>can be</em> (and perhaps would be easier to implement if it would be) <strong>outside</strong> of <code>MagicSquare</code>. The reason that I want it to be inside <code>MagicSquare</code> is perhaps a gut feeling. I have a hunch that it would be more clean and tidy this way because the logic of determining which kind of Magic Square a certain Magic Square is, seems to me, to belong in the <code>MagicSquare</code> class.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COThis smells like an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Why do you want to re-class it? Why not use a "factory function" (or, equivalently, a `@classmethod` as an alternate constructor) that constructs an object of the right class in the first place? Or a `__new__` method? Why wait until you've gotten to `__init__` and then change the class?
      singulars
    2. CO@abarnert I don't specifically want to re-class. I want the code that determines which subclass the object will be an instance of to be in the top, `Car`, class. Is there a reason for it not to be in `__init__`? Would it be better in `__new__`?
      singulars
    3. COYes, there is a reason for it not to be in `__init__`. Because by the time you've gotten into the base's `__init__`, it's obviously too late to just construct the object normally because you're already in the middle of object construction. `__new__` would probably be better than `__init__`, but an alternate constructor `@classmethod` (or some other variation on the theme—a factory, a Cocoa-style "class cluster" constructor, …) would probably be even _better_.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload