更新时间:2023-11-30 18:30:04
尽管我不建议这样做-请参阅我对原始问题的评论-嵌套结构通常要求递归下降.您将编写一个函数来检查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)