Setting class variable based on another class' variable
<p>I was trying to remind myself of OO programming by creating some kind of chess clone in python and found myself with the current issue.</p>

<p>I'd like to give each piece on the board an 'identifier' variable so that it can be displayed on screen e.g : bR would indicate a black rook. However I'm not sre how I can establish this due to colour being a variable being inherited to the rook class, rather than being a part of it.</p>

<p>This is probably a silly question but I've also looked into using the __str__ function for this display but I run into a similar issue that way too. The code I have is below.</p>

<p>I could use another __init__ variable but I'd rather not have to specify every single pawns identifier (P) when the pawn class could surely do the same for me?</p>

<pre><code>class piece(object):
    identifier = ' ' 

    def __init__(self, colour):
        self.colour = colour

class rook(piece):

    identifier = 'R'

rookA = rook('b')
</code></pre>

<p>And I'm looking to achieve:</p>

<p><code>print(rookA.identifier)
"bR"</code></p>

<p>or </p>

<p><code>print(rookA)
"bR"</code></p>

<p>if I'm going about this task in the wrong way, please suggest an alternative!</p>

<p>Thanks</p>

<p>I'm not sure what issue you had with using <code>__str__</code>, but that is absolutely the right way to go about it.</p>

<pre><code>class Piece(object):
    identifier = ' ' 

    def __init__(self, colour):
        self.colour = colour

    def __str__(self):
        return "{}{}".format(self.colour, self.identifier)
</code></pre>

python python-3.x