1

I'm doing a project on the board game Battleship. Because 0, 1, 2, 3 ... is 1, 2, 3 ,4 ... (I don't know what to call it), I need the variable row and column to be one less than what they are, ie:

row -= 1
column -= 1

I need to use this in a bunch of functions, so I thought it would be neater if I just have a helper function each time instead. I've tried these two methods:

row, column = row -= 1, column -= 1

However, this gives me a syntax error on the column -= 1.

I also tried:

def decimal_helper(row, column):
    row -= 1
    column -= 1
    return row, column

However, this just returns a tuple, while I need the two variables to be returned separately.

Forge
  • 6,538
  • 6
  • 44
  • 64
Long Vuong
  • 181
  • 1
  • 1
  • 9
  • The only thing I can decipher form this, is that I think you are looking to do this: `a, b = decimal_helper(row, column)`. If you are returning multiple items from your method, it is returning as expected. You need to unpack accordingly. Other than that, I have no idea what else you are asking. – idjaw Mar 07 '16 at 16:29
  • 2
    You could do `row, column = row - 1, column - 1`, but why not stick with what you have? – jonrsharpe Mar 07 '16 at 16:30
  • *All* functions return exactly one value; try to how the caller would access two return values without something that looks like tuple unpacking anyway. – chepner Mar 07 '16 at 16:33

2 Answers2

2
def decimal_helper(row, column):
   return row - 1, column -1

# unpack the tuple back into two variables
row, column = decimal_helper(row, column)

You can also go for a more elegant way and use one-line as @jonrsharpe suggested:

row, column = row - 1, column - 1
Forge
  • 6,538
  • 6
  • 44
  • 64
0

I find this to be more elegant. This way you encapsulate the "conversion" logic an don't need to replicate it throughout your code:

class DecimalHelper:

    def __init__(self, row, column):
        self.row = row
        self.column = column

        self.rowIndex = row - 1
        self.colIndex = column - 1


row = 3
column = 4

dc = DecimalHelper(row, column)

print dc.rowIndex
print dc.colIndex

And there is an even more elegant way, using a class property (credits to this post):

class ClassPropertyDescriptor(object):

    def __init__(self, fget, fset=None):
        self.fget = fget
        self.fset = fset

    def __get__(self, obj, klass=None):
        if klass is None:
            klass = type(obj)
        return self.fget.__get__(obj, klass)()

    def __set__(self, obj, value):
        if not self.fset:
            raise AttributeError("can't set attribute")
        type_ = type(obj)
        return self.fset.__get__(obj, type_)(value)

    def setter(self, func):
        if not isinstance(func, (classmethod, staticmethod)):
            func = classmethod(func)
        self.fset = func
        return self    

def classproperty(func):
    if not isinstance(func, (classmethod, staticmethod)):
        func = classmethod(func)

    return ClassPropertyDescriptor(func)

class DecimalHelper:

    def __init__(self, row, column):
        self.row = row
        self.column = column

    @classproperty
    def rowIndex(cls):
        return row - 1

    @classproperty
    def colIndex(cls):
        return column - 1

row = 3
column = 4

dc = DecimalHelper(row, column)

print dc.rowIndex
print dc.colIndex
Community
  • 1
  • 1
EduardoCMB
  • 392
  • 2
  • 17