Can I dynamically convert an instance of one class to another?

前端 未结 4 799
灰色年华
灰色年华 2020-12-03 07:37

I have a class that describe chess pieces. I make for all type piece in the Board a class for example Pawn, Queen, keen, etc... I have a trouble in Pawn class I want to conv

4条回答
  •  甜味超标
    2020-12-03 07:39

    An object in Python will always be an instance of its class. There is no way to change that without resorting to "dirty hacks".

    In your case, you should probably consider refactoring the logic. Instead of letting a piece move itself, define some kind of controller that moves the pieces. If a pawn reaches the last row, this controller can easily replace a piece with another.

提交回复
热议问题