12

[Sorry, I'm new in Python. Although it seems to be a very basic question, I did my share of due diligence before asking this audience, trying to avoid really stupid questions].

I'm trying to figure out the correct idiom for returning an l-value from a function. Assume I've a container of 64 objects, and I want to be able to return a reference to these objects.

class ChessBoard:
    def __init__(self):
        self.squares = [None for x in range(64)]

    square( row, col ):
        return self.squares(row*8+col)    <---- I'd like this to be l-value

Then, from outside the class I want to:

board = ChessBoard()
board.square(0,0) = Piece( Shapes.ROOK, Colors.White )    <-- I'm getting an error here
board.square(0,1) = Piece( Shapes.BISHOP, Colors.White )
... etc.

So, I would like the function 'at' to return a lvalue (Something like a reference in C++), but I can't find anything resembling a reference or a pointer in the language. If I stored a list in each square containing one Piece, it is possible I could do something like: board.square(0,0)[0] = Piece - but it seems crazy (or maybe not - as I said, I'm new to the language).

How would you approach this data structure?

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
Uri London
  • 10,631
  • 5
  • 51
  • 81

4 Answers4

10

In Python, everything is a reference. The only problem is that None is immutable, so you can't use the returned reference to change the value.

You also can't override the assignment operator, so you won't get this particular kind of behaviour. However, a good and very flexible solution would be to override the __setitem__ and __getitem__ methods to implement the subscription operator ([]) for the class:

class ChessBoard(object):
  def __init__(self):
    self.squares = [None] * 64

  def __setitem__(self, key, value):
    row, col = key
    self.squares[row*8 + col] = value

  def __getitem__(self, key):
    row, col = key
    return self.squares[row*8 + col]

Usage:

>>> c = ChessBoard()
>>> c[1,2] = 5
>>> c[1,2]
5
Niklas B.
  • 92,950
  • 18
  • 194
  • 224
  • Note that the definition is slightly more ugly in 3.x because the special case of tuple unpacking in argument lists went the way of the norwegian blue. You'd have to define `def __setitem__(self, row_col, value): row, col = row_col; self.squares[row * 8 + col] = value`. –  Apr 01 '12 at 14:39
  • @delnan: Thanks, I changed it because the rest is version-agnostic. I think the choice to remove this feature is unfortunate, it can be really useful if used sensibly. – Niklas B. Apr 01 '12 at 14:40
2

You can try something like this, at the cost of having to put bogus [:] indexers around:

class Board:
    def __init__(self):
        self.squares=[None for x in range(64)]
    def square(self, row, col):
        squares=self.squares
        class Prox:
            def __getitem__(self, i):
                return squares[row*8+col]
            def __setitem__(self, i, v):
                squares[row*8+col]=v
        return Prox()

Then you can do

b=Board()
b.square(2,3)[:]=Piece('Knight')
if b.square(x,y)[:] == Piece('King') ...

And so on. It doesn't actually matter what you put in the []s, it just has to be something.

(Got the idea from the Proxies Perl6 uses to do this)

clsn
  • 21
  • 1
1

(Not answering your question in the title, but your "How would you approach this data structure?" question:) A more pythonic solution for your data structure would be using a list of lists:

# define a function that generates an empty chess board
make_chess_board = lambda : [[None for x in xrange(8)] for y in xrange(8)]

# grab an instance
b = make_chess_board()

# play the game!
b[0][0] = Piece(Shapes.ROOK, Colors.White)
b[0][1] = Piece(Shapes.BISHOP, Colors.White)

# Or use tuples:
b[0][0] = (Shapes.ROOK, Colors.White)
b[0][1] = (Shapes.BISHOP, Colors.White)
Udi
  • 29,222
  • 9
  • 96
  • 129
1

As Niklas points out, you can't return an l-value.

However, in addition to overriding subscription, you can also use properties (an application of descriptors: http://docs.python.org/howto/descriptor.html) to create an object attribute, which when read from, or assigned to, runs code.

Marcin
  • 48,559
  • 18
  • 128
  • 201
  • That doesn't solve this particular problem, though, because there's no attribute setting to intercept here. It could however be used to enable something like `board.square(0,0).value = Piece(...)`. – Niklas B. Apr 01 '12 at 14:56
  • Not true. I found a way using globals() and locals(). See [here](http://stackoverflow.com/questions/11748780/is-it-possible-something-like-lvalue-of-perl-or-setf-of-lisp-in-python/11750015#11750015) – Logan Jul 31 '12 at 22:38
  • @Logan True. You did not find a way to return an lvalue, you found a way to set a value in the locals/globals dictionary. – Marcin Jul 31 '12 at 22:50