NameError: global name 'btn_Matlab' is not defined ?

S

Stef Mientki

hello,

Never seen this before and I've no explanation whatsoever (Python 2.6)

I've some dynamic generated code,
one of objects generated is a wx.Button, called 'btn_Matlab'.

After the code is generated, I can see that the button is created in the local namespace

print locals()['btn_Matlab']

<wx._controls.Button; proxy of <Swig Object of type 'wxButton *' at 0x3602d28> >

but if I try to print the button (at exactly the same location), I get an error

print btn_Matlab

NameError: global name 'btn_Matlab' is not defined ?

Why isn't the compiler first checking the local namespace ?
any clues ?

thanks,
Stef Mientki
 
S

Steven D'Aprano

hello,

Never seen this before and I've no explanation whatsoever (Python 2.6)

I've some dynamic generated code,
one of objects generated is a wx.Button, called 'btn_Matlab'.

How do you generate the code?

What code is generated? What does it do?

After the code is generated, I can see that the button is created in the
local namespace

print locals()['btn_Matlab']

<wx._controls.Button; proxy of <Swig Object of type 'wxButton *' at
0x3602d28> >

but if I try to print the button (at exactly the same location), I get
an error

print btn_Matlab

NameError: global name 'btn_Matlab' is not defined ?

Why isn't the compiler first checking the local namespace ? any clues ?


My guess is that you've declared btn_Matlab as global, and then
dynamically created a local with the same name using exec or similar.
 
S

Stef Mientki

hello,

Never seen this before and I've no explanation whatsoever (Python 2.6)

I've some dynamic generated code,
one of objects generated is a wx.Button, called 'btn_Matlab'.
How do you generate the code?

What code is generated? What does it do?

After the code is generated, I can see that the button is created in the
local namespace

print locals()['btn_Matlab']

<wx._controls.Button; proxy of <Swig Object of type 'wxButton *' at
0x3602d28> >

but if I try to print the button (at exactly the same location), I get
an error

print btn_Matlab

NameError: global name 'btn_Matlab' is not defined ?

Why isn't the compiler first checking the local namespace ? any clues ?

My guess is that you've declared btn_Matlab as global, and then
dynamically created a local with the same name using exec or similar.

thanks Stevem,

I found a solution.
I indeed tried to "inject" local variables from a stack a few levels deeper,
which doesn't seem to be possible (or reliable).
The documentation isn't overwhelming about that point, exec and eval doesn't mention anything,
but execfile warms for this issue.
The code now looks something like this:

self.p_locals = sys._getframe ( StackUp ).f_locals
self.p_globals = sys._getframe ( StackUp ).f_globals

Component = eval ( defi[1], self.p_globals, self.p_locals ) ( Parent, **Extra )

self.p_globals[ 'Component' ] = Component
if 'self' in defi[0] :
exec ( '%s = Component' %( defi[0] ), self.p_globals, self.p_locals )
else :
exec ( '%s = Component' %( defi[0] ), self.p_globals)

cheers,
Stef
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top