Juanvulcano
Juanvulcano

Reputation: 1396

How to copy a class variable - Python

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

Upvotes: 4

Views: 5752

Answers (2)

Mad Physicist
Mad Physicist

Reputation: 114300

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.

Upvotes: 3

William
William

Reputation: 2935

Maybe you can try with deepcopy, like this:

from copy import deepcopy

And then change:

calculations = status

To:

calculation = deepcopy(status)

Upvotes: 3

Related Questions