#exec() won't work (don't work like i think)

1 messages · Page 1 of 1 (latest)

daring forge
#

Hi, I have part of my code that does this (see ss)

function is for example equal to y=x. My goal of this piece of code is therefore to give the value of the left member (here x) to y from a variable of type string using exec().

The problem is that according to my command editor, exec() in my code "does nothing" (it does not give a new value to y
anyone know what to do?

viscid badger
#

function()? why are u using exec

#

exec (as far as i know) is used to run functions through a 'string format'. unless you're using user input, why exec?

daring forge
viscid badger
#

once again

#

why

daring forge
# viscid badger *why*

function is type by the user (for a well particular reason) and verify by the program that its well a mathematic function (and not a try of code insertion)

viscid badger
#

is y a global variable?

daring forge
#

i never declared y as a global

#

i tried on my cmd and it worked

daring forge
daring forge
#

here's the program with x and y global (this also don't work)

viscid badger
#

using exec will not update the local variable of y, just the y variable within that exec function

daring forge
#

you mean x

#

cuz i defined y on the exec()

#

(function = "y=x")

viscid badger
#

k hold on, ive never used exec so ill try

daring forge
#

k

#

thx

daring forge
#

and x or y is not global here (i think)

#

but it work

#

whytf

viscid badger
#

so your setup for function is

#
def function(self):
    return "y = x"
#

for function?

daring forge
#

hmm its not writed like this on my code but i think that its the same

#

on my code...

#

function = user_input

#

(user_input is then a string)

viscid badger
#

yeah you can't cross-reference variables like this

#

the function call will not change the y variable of the function where you call it from

#

so you'd have to make it a global one in the call

#

aka

#
exec(function, globals())
daring forge
#

and i put x and y in globals ?

#

like this ?

#

exec(function, globals(x, y))

viscid badger
#

no

#

you just do what i sent

#

and it will make the provided variable within function as global

daring forge
viscid badger
#
exec(function, globals())
#

this.

daring forge
#

k

#

i'll try

viscid badger
#

you still have to make x global if you want to reference it

#

ie, "y = x"

daring forge
#

i think that's worrking

#

thank you 🥹