4

I'm making a game that uses a board of 4x4 and I want to check whether a card have already been pressed or not.

For that, I am using two boards. One that will be used for calculations (The variable I want to copy) and the other one that will be used for display in the game (Original board called status).

I have the following piece of code and I want to copy the status variable of TreasureHuntGrid and use it in another function of the same class. It should be independent from the variable it is being copied, so a change in the status variable won't affect the calculations variable.

I think the code I have here handles the status and calculations variable as it is the same.

How can I treat them both independently?

class TreasureHuntGrid(GridLayout):
    Finish.shuffle()
    status = ListProperty(Finish.board) #Return a random lists of lists with 1 and -1 
    calculations = status 
    def __init__(self, *args, **kwargs):
      super(TreasureHuntGrid, self).__init__(*args, **kwargs)

    def button_pressed(self, button):
        if self.calculations[row][column] != 2: #Check if it is pressed or not
            button.text = {-1: Finish.surrounded(row, column), 1: 'X'}[self.sta$
            button.background_color = colours[self.status[row][column]]
            self.calculations[row][column] = 2
1
  • If you don't want the values to be shared by all members of the class, you shouldn't make them class variables. Use instance variables instead. Commented Dec 9, 2015 at 19:10

2 Answers 2

3

Everything in Python is a namespace. When you define status = ... in the class definition, the class object gets a new attribute: TreasureHuntGrid.status. You want to place the status and calculations attributes into the object namespace. To do this, just define self.status and self.calculations in __init__():

def __init__(self, *args, **kwargs):
  super(TreasureHuntGrid, self).__init__(*args, **kwargs)
  status = ListProperty(Finish.board) #Return a random lists of lists with 1 and -1 
  calculations = status 

Additionally, note that Finish.shuffle() will only be called once when the module is imported. If this is not what you intended, place that line of code into your constructor as well.

The confusion you had probably arose from the way Python determines what self.status means. If you are reading the field, self.status will redirect to the parent namespace if it is not defined in the object's namespace: TreasureHuntGrid.status. If you are assigning to self.status and it does not exist, it gets created. To see what I mean, you may want to study the following example:

>>> class A:
...    a = 1
...
>>> print A.a
1
>>> obj = A()
>>> print obj.a
1
>>> obj.a = 2
>>> print A.a
1
>>> print obj.a
2

If you are using Python 3, put parentheses around the arguments of print.

Sign up to request clarification or add additional context in comments.

2 Comments

How do I access attributes in the object namespace? Instead of using self.status what should I use?
You access it with self.status. The confusion you may be having is caused by what self.status does when there is no attribute status in the object but there is one in the class. I have attempted to address the issue in my edit.
3

Maybe you can try with deepcopy, like this:

from copy import deepcopy

And then change:

calculations = status

To:

calculation = deepcopy(status)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.