jdf / Processing.py-Bugs

A home for all bugs and feature requests about Python Mode for the Processing Development Environment.
41 stars 8 forks source link

Problems with Processing Python's 'exec' #223

Closed FarukHammoud closed 6 years ago

FarukHammoud commented 6 years ago

A normal Python interpreter returns 2 when I execute : exec('def Soma(n): return n \ndef Soma2(n): return Soma(n) \nprint(Soma2(2)) \n')

But it doesn't work with Processing Python's 'exec', it return an error : global name 'Soma' is not defined

Someone knows why?

jdf commented 6 years ago

When I run this program I see 2.

FarukHammoud commented 6 years ago

It only works when you run it out of 'setup' or 'draw'.

jdf commented 6 years ago

The same program will fail in the same way in CPython as well.

def foo():
    exec('def A(n): return n\ndef B(n): return A(n)\nprint(B(12)) \n')
foo()

NameError: global name 'A' is not defined

Hint:

def foo():
    exec('global A\ndef A(n): return n\ndef B(n): return A(n)\nprint(B(12)) \n')
foo()