Python Forum
exec in a function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: exec in a function (/thread-33356.html)



exec in a function - paul18fr - Apr-18-2021

Hi

I'm modifying a code by moving an "exec" inside a function creating an error; I wrote a small code highlighting what I'm facing.

What I'm missing and what is the correct programming ?

Thanks

What I'm missing

Sentence = "World is "

print("without using a function")
Add0 = 'beautifull'
exec("New0 = Sentence + '%s'" %Add0)
print("\t- Complete sentence 0 = {}".format(New0))

print("\nUsing a function")
def Complete(Part):
    Add = 'beautifull'
    exec("New = Part + '%s'" %Add)
    print("\t- Complete sentence 1 = {}".format(New))
    print("\t- Complete sentence 2 = %s" %(New))
    
Complete(Sentence)



RE: exec in a function - Yoriz - Apr-18-2021

You should include the error in your post
Error:
NameError: name 'New' is not defined
New is not the same as New0


RE: exec in a function - paul18fr - Apr-18-2021

(Apr-18-2021, 07:48 PM)Yoriz Wrote: You should include the error in your post
you're right, I'll do next time

I'm trying to understand why outside the function exec works (with New0) but not inside (with New that has no relation with New0); I'm missing things but I've not understood what so far


RE: exec in a function - Yoriz - Apr-18-2021

Sorry, i didn't notice the exec("New = Part + '%s'" %Add).

See this link

Sentence = "World is "
 
print("without using a function")
Add0 = 'beautifull'
exec("New0 = Sentence + '%s'" %Add0)
print("\t- Complete sentence 0 = {}".format(New0))
 
print("\nUsing a function")
def Complete(Part):
    Add = 'beautifull'
    ldict = locals()
    exec("New = Part + '%s'" %Add,globals(),ldict)
    New = ldict['New']
    print("\t- Complete sentence 1 = {}".format(New))
    print("\t- Complete sentence 2 = %s" %(New))
     
Complete(Sentence)
Output:
without using a function - Complete sentence 0 = World is beautifull Using a function - Complete sentence 1 = World is beautifull - Complete sentence 2 = World is beautifull



RE: exec in a function - paul18fr - Apr-19-2021

Thanks Yoriz for pointing out the issue.

Another solution seems to use globals() function as a dictionnary (key + value)

Sentence = "World is "
 
print("without using a function")
Add0 = 'beautifull'
exec("New0 = Sentence + '%s'" %Add0)
print("\t- Complete sentence 0 = {}".format(New0))
 
print("\nUsing a function")
def Complete(Part):
    Add = 'beautifull'
    globals()["New"] = Part + Add
    print("\t- Complete sentence 1 = {}".format(New))
    print("\t- Complete sentence 2 = %s" %(New))
     
Complete(Sentence)
Output:
without using a function - Complete sentence 0 = World is beautifull Using a function - Complete sentence 1 = World is beautifull - Complete sentence 2 = World is beautifull



RE: exec in a function - ndc85430 - Apr-19-2021

Why do you want to use exec in the first place? Writing code in strings that you glue together just makes it harder to understand the program. Sure, there are likely cases where you do need exec, but I think those are quite rare.


RE: exec in a function - paul18fr - Apr-19-2021

(Apr-19-2021, 07:59 AM)ndc85430 Wrote: Why do you want to use exec in the first place?

It's a basic code especially created to highlight the issue I was confronted to Wink