VictorLegros
VictorLegros

Reputation: 442

Python colon ":" operator applied to a variable?

I received a Python class defining a game "Board" object, as well as a number of members which are initialized within a constructor. Most of this is straightforward, but I don't understand the meaning of the ":" operator in the initialisations of the sets in the last two lines of this snippet:

class Board:
BLANK = 0
NOT_MOVED = (-1, -1)

__active_players_queen1__= None
__inactive_players_queen1__= None
__active_players_queen2__= None
__inactive_players_queen2__= None


def __init__(self, player_1, player_2, width=7, height=7):
    self.width=width
    self.height=height

    self.queen_11 = "queen11"
    self.queen_12 = "queen12"
    self.queen_21 = "queen21"
    self.queen_22 = "queen22"

    self.__board_state__ = [ [Board.BLANK for i in range(0, width)] for j in range(0, height)]
    self.__last_queen_move__ = {self.queen_11:Board.NOT_MOVED, self.queen_12:Board.NOT_MOVED, self.queen_21:Board.NOT_MOVED, self.queen_22:Board.NOT_MOVED}
    self.__queen_symbols__ = {Board.BLANK: Board.BLANK, self.queen_11:11, self.queen_12:12, self.queen_21:21, self.queen_22:22}

E.g. what does "self.queen_11:11" mean?

I'm relatively inexperienced Python, and it looks similar to "dot notation" for member access, or how some languages handle assignment (e.g. ":="), but from my understanding of Python, dot notation is dot notation, and assignment is a single equals sign.

My reading is the queen_11 variable (initialized earlier to the string "queen11") is somehow associated with the integer value 11?

This doesn't seem to be a variant of list slicing or annotations. This is Python 2.7 code BTW.

Upvotes: 2

Views: 9184

Answers (1)

VictorLegros
VictorLegros

Reputation: 442

Credit to https://stackoverflow.com/users/67579/willem-van-onsem for his comment: looks like this is a way of initializing Dictionaries which I was not familiar. I've only done it the other two ways shown here: https://developmentality.wordpress.com/2012/03/30/three-ways-of-creating-dictionaries-in-python/

Upvotes: 1

Related Questions