Python:如何在给定时间调用字典包含的可调用对象? [英] Python : How to call dictionnary-contained callables at a given time?

查看:72
本文介绍了Python:如何在给定时间调用字典包含的可调用对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用包含大量键的python中的字典对象.它们的某些关联值类型是可调用类型.

I'm working with a dictionary object in python which contains numerous keys. Some of their associated value type is of callable type.

类似的东西:

dico = {
    'key1' : 1,
    'key2' : 'cars',
    'key3' : <bound method MyClass.MyMethod_A of <MyClass: MyClass object >>,
    'deeperkeys':{
                  subkey1 : 'space',
                  subkey2 : <bound method MyClass.MyMethod_B of <MyClass: MyClass object >>,
                 },
       }

我知道我可以做到:

dico['key3'] = dico['key3']()
dico['deeperkeys']['subkey2'] = dico['deeperkeys']['subkey2']()

此外,请注意,如上所述,我不是在unic dico上工作,而是在大量类似dico的字典上工作.另外,有时我不知道必须调用哪个键. 区别和超越性地调用所有可调用对象的最佳方法是什么?

Furthermore, notice that i do not work on a unic dico as presented above, but on a huge list of dico-like dictionaries. Also, i sometimes do not know which keys must be called. What is the best way to discriminately and transcendentally call all callables ?

在乔恩·基帕斯基(Jon Kiparsky)之后,我在每个方法定义上方添加了@property装饰器.它通过将问题倒过来解决了问题.没有可调用项,但使用此装饰器调用了MyClass的源定义的方法.而我的词典列表就是从中生成的!

Following Jon Kiparsky, i added a @property decorator above each method definition. And it did the trick by taking the problem upside down. No callables but called methods with this decorator to the source definition of MyClass. And my lists of dictionaries are generated from it !

推荐答案

尽管我不建议这样做-请参阅我对原始问题的评论-嵌套结构通常要求递归下降.您将编写一个函数来检查dict的values()列表中的每个值,并执行以下三种操作之一:

While I don't recommend this by any means - see my comment on the original question - nested structures typically call for recursive descent. You'd write a function that examines each of the values in a dict's values() list and does one of three things:

  • 如果该值是可调用的,请调用它
  • 如果值是字典,请进行递归调用
  • 否则,请跳过它.

确实,与探索文件树没有太大不同.

Not very different from exploring a file tree, really.

但是,对于重复的问题,我并不表示歉意,重要的是在这里查看您的实际问题,并确定这是否真的是您想做的事情.我看不到这是一个很好的解决方案的问题.

However, and I don't apologize for repeating this, it is important to review your actual problem here and decide if this is really a thing you want to do. I can't see what problem this is a good solution for.

基于来自OP的更多信息,看来@property装饰器可能是必需的.用法示例:

based on further information from the OP, it looks like the @property decorator may be what's required. Example of usage:

>>> class Foo:
...     @property
...     def current_time(self):
...             from datetime import datetime
...             return datetime.now()
... 
>>> f = Foo()
>>> f.current_time
datetime.datetime(2014, 12, 10, 15, 29, 35, 146096)
>>> f.current_time
datetime.datetime(2014, 12, 10, 15, 29, 42, 383874)

请注意,语法将current_time名称视为一个简单值,但返回的值是调用该函数的结果,导致求值时的值,我认为这就是您的意思.再之后.

Note that the syntax evaluates the current_time name as if it were a simple value, but the value returned is the result of calling the function, resulting in the value at the time of evaluation, which I think is what you're after.

(但正如@abarnert观察到的那样,您正在将它们放入字典中,因此这对您不起作用)

(but as @abarnert observes, you're putting these into a dictionary, so this isn't going to work for you)

这篇关于Python:如何在给定时间调用字典包含的可调用对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆