Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
exec() in class, NameError
#1
Hi. I've got a problem with exec() function in my class. If code is like:
    def rysuj(self, frame, tuptus):
        for bu in tuptus:
            def foo():
                print(self.x_coord)
                exec('self.'+'y_coord')
            foo()
everything is fine, but if i delate print:
    def rysuj(self, frame, tuptus): 
        for bu in tuptus:
            def foo():
                exec('self.'+'y_coord')
            foo()
gives me an error
Error:
"C:\Users\karol\anaconda3\envs\porjekt ostateczny\python.exe" "C:\Users\karol\PycharmProjects\porjekt ostateczny\main_menu_by_tkinter_2.py" Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\karol\anaconda3\envs\porjekt ostateczny\lib\tkinter\__init__.py", line 1705, in __call__ return self.func(*args) File "C:\Users\karol\PycharmProjects\porjekt ostateczny\main_menu_by_tkinter_2.py", line 152, in <lambda> command=lambda: self.rysuj(self.ctr_left,przyciski_rysuj), File "C:\Users\karol\PycharmProjects\porjekt ostateczny\main_menu_by_tkinter_2.py", line 177, in rysuj foo() File "C:\Users\karol\PycharmProjects\porjekt ostateczny\main_menu_by_tkinter_2.py", line 176, in foo exec('self.'+'y_coord') File "<string>", line 1, in <module> NameError: name 'self' is not defined
does anybody know why?
Reply
#2
Pass 'self' as parameter.
def rysuj(self, frame, tuptus): 
    for bu in tuptus:
        def foo(self):
            exec('self.'+'y_coord')
        foo(self)
   
Reply
#3
So why it works with 'print(self.x_coord)'?
Reply
#4
Because print function set te locals variables. See below:
def rysuj(self, frame, tuptus): 
    for bu in tuptus:
        def foo():
            print(locals())
            exec('self.'+'y_coord')
        foo()
and
def rysuj(self, frame, tuptus): 
    for bu in tuptus:
        def foo():
            print(self)
            print(locals())
            exec('self.'+'y_coord')
        foo()
Reply
#5
I get it :D Thank you
Reply
#6
why would you have something like this in the class in the first place?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
I tried to use similar way to pack buttons with command in function. I used closure instead.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to avoid exec(), globals(), locals(), eval() paul18fr 10 4,905 Apr-21-2021, 05:53 PM
Last Post: snippsat
  exec in a function paul18fr 6 3,308 Apr-19-2021, 11:10 AM
Last Post: paul18fr
  exec + subprocess = UnboundLocalError paul18fr 6 3,443 Feb-04-2021, 06:27 AM
Last Post: Gribouillis
  NameError when calling a class method mfreudenberg 2 2,254 Sep-25-2020, 07:40 AM
Last Post: mfreudenberg
  Is this use of exec pythonic? psolar 1 1,798 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
  NameError: NameError: global name 'BPLInstruction' is not defined colt 7 4,300 Oct-27-2019, 07:49 AM
Last Post: Larz60+
  common code, def a function vs exec() a string Skaperen 7 3,269 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,640 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