Uri London
Uri London

Reputation: 10797

Returning an lvalue from a function in python

[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?

Upvotes: 12

Views: 3355

Answers (4)

clsn
clsn

Reputation: 21

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)

Upvotes: 2

Udi
Udi

Reputation: 30532

(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)

Upvotes: 1

Niklas B.
Niklas B.

Reputation: 95328

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

Upvotes: 10

Marcin
Marcin

Reputation: 49856

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.

Upvotes: 1

Related Questions