Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
exec in a function
#1
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)
Reply
#2
You should include the error in your post
Error:
NameError: name 'New' is not defined
New is not the same as New0
Reply
#3
(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
Reply
#4
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
Reply
#5
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
Reply
#6
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.
Reply
#7
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to avoid exec(), globals(), locals(), eval() paul18fr 10 4,902 Apr-21-2021, 05:53 PM
Last Post: snippsat
  exec + subprocess = UnboundLocalError paul18fr 6 3,441 Feb-04-2021, 06:27 AM
Last Post: Gribouillis
  exec() in class, NameError niski1996 6 3,870 Apr-20-2020, 07:14 PM
Last Post: niski1996
  Is this use of exec pythonic? psolar 1 1,797 Feb-07-2020, 12:23 PM
Last Post: buran
  problem using exec to save local variables dkarl 0 1,761 Dec-01-2019, 08:52 AM
Last Post: dkarl
  common code, def a function vs exec() a string Skaperen 7 3,267 May-27-2019, 10:13 AM
Last Post: heiner55
  using function through exec() kiyoshi7 9 5,551 Mar-03-2018, 09:25 PM
Last Post: Larz60+
  namespaces in comprehension in exec() Skaperen 4 4,638 Mar-28-2017, 03:31 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020