复制namesp时处理循环依赖(functools>\u functools>functools)

2024-04-24 03:14:31 发布

您现在位置:Python中文网/ 问答频道 /正文

模块functools是指在_functools中定义的reduce。 同时_functools指在functools中定义的partial

我在尝试为monkey补丁创建命名空间副本时遇到了这个问题。但在我的情况下,我需要旧的和猴子修补功能,因此需要一个副本。有什么建议可以这样循环依赖项吗?你知道吗

使用Python 2.7.10

每个请求都有一些源代码

def _get_dependent_modules(m):
  """Return set of all modules defining symbols in given module."""

  modules = set()
  for symbol_name, symbol in m.__dict__.items():
    if hasattr(symbol, '__module__'):
        if symbol.__module__ in sys.modules:
          print "Symbol %s, defined in %s" % (symbol_name, symbol.__module__)
          modules.add(sys.modules[symbol.__module__])
        else:
          print "Cant find module for %s" %(symbol)
  return modules

_get_dependent_modules(functools)

Symbol wraps, defined in functools
Symbol partial, defined in functools
Symbol update_wrapper, defined in functools
Symbol total_ordering, defined in functools
Symbol reduce, defined in _functools
Symbol cmp_to_key, defined in functools

_get_dependent_modules(sys.modules["_functools"])

Symbol partial, defined in functools
Symbol reduce, defined in _functools

Tags: inmodulesreduceget定义sys副本symbol
1条回答
网友
1楼 · 发布于 2024-04-24 03:14:31

到目前为止,你只需记住你所看到的(identity comparison),而不必重复或深入到你所记得的内容中。你知道吗

也许我不了解操作的复杂性(复制命名空间)。你知道吗

只是一个建议:

sub scan_symbols(object):
    for each member in object:
        if member is of type-primitive:
            whatever
        if member is of type-object:
            record member reference if not already recorded
            if reference is new:
                scan_symbols(member)

PHP中的真实代码示例:

https://raw.githubusercontent.com/pradosoft/prado/master/framework/Util/TVarDumper.php

这种方法也避免了循环DEP:

https://github.com/symfony/var-dumper

相关问题 更多 >