Python Function Scope

Here is a question I have about function scope in Python.

If I have a table data, called ‘Global’, and I want to get the value in row 1, column ‘total_amount’

I would access that as such:
total_amount = int(op(‘global’)[1, “total_amount”])

Now, if I want the value of that to be a variable, that I can access inside the “offToOn” function, (in a chop Execute DAT for instance), this is how I have been doing it:

[code]total_amount = int(op(‘global’)[1, “total_amount”])

def offToOn(channel, sampleIndex, val, prev):
global total_amount
print(total_amount)[/code]

This will print the total amount (i.e. the amount in the table). However, if, in the same function I add

total_amount = 5

That number does not get added to the table. Why is? It seems like once I declare the global total_amount, it should reference the (op(‘global’)[1, “total_amount”]) path instead of just its value. Am I wrong here? And, if I do set, in the offToOn function, the value of “global total_amount” to 5, I am assuming that next time the function is called, the value of “total=_amount” will be reset to whatever the table value is, right?

After typing that out I am realizing it should just be Python storage FTW.

So theres 2 things stopping you here.

The first issue is that you’re casting an integer to total_amount, not a reference to the cell.

The second is that here TouchDesigner is automagically giving you the value of the cell when you’re calling the cell, but when you write back to it, you need to specify that you want to address it’s value by using .val

For example, if you got rid of the int(), and made it total_amount.val = 5 your example would work fine.

So depending on your use you may find that using storage is more friendly, or maybe you just cast the cell value to an int when you need it, and leave it as a reference to the cell otherwise.

brilliant.