Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning an lvalue from a function in python

Tags:

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?

like image 930
Uri Avatar asked Apr 01 '12 14:04

Uri


People also ask

Can a function return an lvalue?

Why is it not possible to set a function returning an address while it is possible to set a function that returns a reference. Short answer: You return a pointer by-value and anything returned by-value is (by definition) not an lvalue.

How do you return a value from a function in Python?

The Python return statement is a special statement that you can use inside a function or method to send the function's result back to the caller. A return statement consists of the return keyword followed by an optional return value. The return value of a Python function can be any Python object.

How do you return a string from a function in Python?

A return statement is used to end the execution of the function call and “returns” the result (value of the expression following the return keyword) to the caller. The statements after the return statements are not executed. If the return statement is without any expression, then the special value None is returned.

How do you return a value from a print in Python?

To print a value in Python, you call the print() function. Returning is used to return a value from a function and exit the function. To return a value from a function, use the return keyword.


2 Answers

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
like image 104
Niklas B. Avatar answered Oct 14 '22 19:10

Niklas B.


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)

like image 41
clsn Avatar answered Oct 14 '22 19:10

clsn