Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass my locals and access the variables directly from another function?

Let's say I have this :


def a(dict):
  locals().update(dict)
  print size

def b():
  size = 20
  f(locals())

What do I have to do to access the size variable directly from the a function? I know of :


size = dict["size"]

but I think there should be a more direct way. I tried using locals().update(dict) but it didn't work. Is there a betteer way ?

like image 404
Geo Avatar asked Aug 14 '09 12:08

Geo


People also ask

How do you pass a local variable to another function?

Here is some code: window. onload = function show(){ var x = 3; } function trig(){ alert(x); } trig();

Can I access a local variable in another function?

You can pass a local variable to a function CALLED by the function containing the local variable. You can return the value os a local variable to the function that CALLED the function containing the local variable. You can make a variable GLOBAL by declaring it outside of the functions.


1 Answers

The Python compiler optimizes access to local variables by recognizing at compile time whether the barenames a function is accessing are local (i.e., barenames assigned or otherwise bound within the function). So if you code:

def lv1(d):
  locals().update(d)
  print zap

the compiler "knows" that barename zap is NOT local (not assigned in function lv1) and so it compiles code to access it as a global instead -- whatever d contains won't matter.

If you prefer slow and bloated code, you can defeat the optimization by using an exec inside the function -- when the compiler sees the keyword exec, it KNOWS you're trying to make your code as slow, bloated and buggy as possible, and so it cooperates by not optimizing in any way, just about.

So, the following code works as you desire:

def lv1(d):
  exec ""
  locals().update(d)
  print zap

lv1({'zap': 23})

it emits 23 as you want.

I hope it's clear from the above "deadpan humor" that the technique is not recommended, but I'd better spell it out very explicitly: for the dubious syntactic pleasure of writing print zap in lieu of print locals()['zap'], you ARE paying a hefty price in terms of performance. Still, like all sorts of dangerous tools that can be useful in rare use cases for really experienced guru-level programmers who really truly know what they're doing and why, exec is there, available for you to use (or mis-use) at your whim: Python does NOT stand in your way!-)

like image 126
Alex Martelli Avatar answered Sep 22 '22 12:09

Alex Martelli