Python Forum

Full Version: Cant access variable from anywhere
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4
Hello,

I think i am not asking the right question. I want to be able to acces the variable Z at the end of the script from the beginning.
Here's an example how this play out. It is not a code, the code is too long to be posted.

f = (M - 5) / (M - Z)

sm = S / f

fI = sm + T

Z = fI / Y

In C# you just declare Z; on top private and problem solve.
My code doesn't have a function so i dont know if can go Global.
How can i solve this issue, any pointers?

Thank you
You can 'declare' Z at the top of your script, if you need to, but it will need a value:

Z = 0 for example.
I tried that but if Z = 0, when compiled Z = 0 and not the value it was suppose to return which is an average number.
You mean that Z cannot look into the future and see what it will be? C# can't do that either. You need to describe the problem you are trying to solve. The example does a good job describing why it doesn't work, but a poor job describing how it should work.
Ah, okay. Well it seems (to me) that you need a function for Z, but again, it will need a value

def zed(fl, Y):
    Z = fl / Y
    return Z
You'll need to pass fl and Y to the function with Z = zed(fl,Y)

Not too sure if this is of any help.
In C# its working you can declare private cm on top and the code will run and return the right value. I am not using any function here and cant say if you can go global or how. You see cm make a circle and send back a new value everytime.
Post c# code.

Next time start with "I have this C# code that I am trying to translate to Python. #CODE".
Its pretty much the same code. I dont like to post because its proprietary stuff and a personnal project and its a long code.
That code isn't any way similar at all to the Python code.

I think you want to remember a value from a previous invocation of a function? That is what the C# code does. Actually the posted code doesn't do that, but I assume that found_index is assigned a value elsewhere and is not always 0. The cm variable does nothing at all and must be used to store the result of the last invocation so it can be used elsewhere.

I see nothing that is any different than how Python variables work. rob101's first example does exactly the same thing.
Z = 0
f = (M - 5) / (M - Z)
sm = S / f
fI = sm + T
Z = fI / Y
Sure, Z starts out as zero, but if you ran lines 2 through 5 again, the second iteration would use the updated value for Z.
Z = 0
for _ in range(2):
    f = (M - 5) / (M - Z)
    sm = S / f
    fI = sm + T
    Z = fI / Y

Or it could be a function, and call the function multiple times.
Z = 0

def func(M, S, T, Y):
    f = (M - 5) / (M - Z)
    sm = S / f
    fI = sm + T
    Z = fI / Y

Z = func(M. S, T, Y) 
Z = func(M. S, T, Y) 
Maybe it should be a class. A class is, at it's foundation, is a construct for binding variables to a set of functions.
class Whatever:
    def __init__(self):
        self.z = 0

    def update(self, m, s, t, y):
        f = (m - 5) / (m - Z)
        sm = s / f
        fI = sm + t
        self.z = fI /y 
Now it looks just like the C# code. It has a class. It has attributes, it has a method.
Already try something like that, python wont let me recycle the variable Z value. I dont even see why it needs a function anyway. Make one function for that make a pretty loaded method.
Pages: 1 2 3 4