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

前端 未结 5 815
没有蜡笔的小新
没有蜡笔的小新 2020-12-10 07:17

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 acc

5条回答
  •  遥遥无期
    2020-12-10 08:06

    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!-)

提交回复
热议问题