English 中文(简体)
粉碎封闭和更换周围范围
原标题:Python closures and replacing surrounding scope

I know when using Groovy closures, I can change the delegate on the closure so function calls made within the closure could be defined externally.

我能否在沙捞?

具体来说,如果你采用以下守则:

def configure():
  build()

def wrap(function):
  def build():
    print  build 

  function()

wrap(configure)

我喜欢印刷(只改动<代码>wrap()。

一些说明:

I don t want to pass functions into configure() since there may be a large number of functions that can be called by configure().

我也不想在全球界定这些职能,因为再次可能有许多职能可以通过<代码>configure(<>configure(>),我不想污染全球名称空间。

最佳回答

Whether or not is a good way to do this is debatable, but here s a solution that doesn t modify the global namespace.

def configure():
  build()

def wrap(f):
  import new
  def build():
    print  build 

  new.function(f.func_code, locals(), f.func_name, f.func_defaults, f.func_closure)()

wrap(configure)

见https://stackoverflow.com/questions/1142068/how-to-modification-the- local-namespace-in-python。 • 如何在python修改当地名称空间

问题回答

This is doable without metaprogramming. Have configure take the build function as a parameter:

def default_build():
    print "default build"

def configure(build_func=None):
    build_func = build_func or default_build
    build_func()

def wrap(func):
    def build():
        print "wrap build"

    func(build)

wrap(configure)

这样就明确了<代码>configure功能的改变。

您可与名称空间相左<代码>configure。 同样,我理解格罗维耶所做的事情:

def build():
    print "default build"

def configure():
    build()

def wrap(func):
    def _build():
        print "wrap build"

    old_build = func.func_globals[ build ]
    func.func_globals[ build ] = _build
    func()
    func.func_globals[ build ] = old_build

一种方式是将<代码>build<>/code>作为全球编码,载于wrap:

def configure():
  build()

def wrap(function):
  global build
  def build():
    print  build 

  function()

wrap(configure)

然而,我没有真正建议这样做,因为它将污染名称空间。

You need to use the global statement so that build() is defined in the global scope, try the following:

def wrap(function):
  global build
  def build():
    print  build 

  function()




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签