python类变量按值查找(python class variable lookup by value)

编程入门 行业动态 更新时间:2024-10-28 13:22:19
python类变量按值查找(python class variable lookup by value)

我有一个带有静态变量的类,用于查找错误/状态代码。 以HTTP状态代码为例

class Foo(object): OK = 200 Not_Modified = 304 Forbidden = 403 Internal_Server_Error = 500

现在我需要根据代码(200,403等)检索语音状态('OK','Not_Modified'等)。 我无法修改类的结构,因为其他程序正在使用它。 所以我创建了一个包含{code : description}的字典description_by_val :

from collections import Hashable class Foo(object): OK = 200 Not_Modified = 304 Forbidden = 403 Internal_Server_Error = 500 description_by_val = dict((value, key) for key, value in locals().iteritems() if not key.startswith("__") and value and isinstance(value, Hashable)) >>> Foo.description_by_val[200] 'OK'

现在我在性能和代码实践方面有疑问。

每次调用Foo.description_by_val都会导致字典被重新生成? 即使数据集非常小,这也不好,因为这会得到数百万次的通话。 这种做法只是不好的做法? 我不想手动手动创建字典,我认为它应该是一个静态变量。

任何想法?

更新:

我的同事刚刚向我指出,我可以在创建description_by_val期间打印一些东西,以确定它是否会被重新生成。

>>> from collections import Hashable >>> >>> def show(key): ... print key ... return True ... >>> >>> class Foo(object): ... OK = 200 ... Not_Modified = 304 ... Forbidden = 403 ... Internal_Server_Error = 500 ... description_by_val = dict((value, key) ... for key, value in locals().iteritems() ... if not key.startswith("__") and key and isinstance(value, Hashable) and show(key)) ... OK Forbidden Internal_Server_Error Not_Modified >>> >>> Foo.description_by_val {200: 'OK', 304: 'Not_Modified', 403: 'Forbidden', 500: 'Internal_Server_Error'} >>> Foo.description_by_val {200: 'OK', 304: 'Not_Modified', 403: 'Forbidden', 500: 'Internal_Server_Error'} >>> Foo.description_by_val[200] 'OK'

我现在很高兴我不必担心性能问题。 我想找出为什么它表现得像这样:)

I have a class with static variables for error/status code lookup. Take HTTP status code as example

class Foo(object): OK = 200 Not_Modified = 304 Forbidden = 403 Internal_Server_Error = 500

Now I need to retrieve the verbal status ('OK', 'Not_Modified', etc) base on the code (200, 403, etc). I can't modify the structure of the class since other programs are using it. So I created a dictionary description_by_val that contain the {code : description}:

from collections import Hashable class Foo(object): OK = 200 Not_Modified = 304 Forbidden = 403 Internal_Server_Error = 500 description_by_val = dict((value, key) for key, value in locals().iteritems() if not key.startswith("__") and value and isinstance(value, Hashable)) >>> Foo.description_by_val[200] 'OK'

Now I have questions in terms of performance and code practice.

Every time I call Foo.description_by_val will it cause the dictionary be regenerated? this is not good even the data set is very small, because this will get call millions times. It that approach simply bad practice? I don't want to manually create the dictionary by hand and I think it should be a static variable.

any thought?

update:

My colleague just pointed out to me that I could print something during the creation of description_by_val to find out if it will be regenerated.

>>> from collections import Hashable >>> >>> def show(key): ... print key ... return True ... >>> >>> class Foo(object): ... OK = 200 ... Not_Modified = 304 ... Forbidden = 403 ... Internal_Server_Error = 500 ... description_by_val = dict((value, key) ... for key, value in locals().iteritems() ... if not key.startswith("__") and key and isinstance(value, Hashable) and show(key)) ... OK Forbidden Internal_Server_Error Not_Modified >>> >>> Foo.description_by_val {200: 'OK', 304: 'Not_Modified', 403: 'Forbidden', 500: 'Internal_Server_Error'} >>> Foo.description_by_val {200: 'OK', 304: 'Not_Modified', 403: 'Forbidden', 500: 'Internal_Server_Error'} >>> Foo.description_by_val[200] 'OK'

I'm now happy with that I don't have to worry about the performance. I want to find out why it behave like this :)

最满意答案

你的想法很合理。 字典不是每次都重新生成,只是第一次创建它。 查找是高效可靠的,这不太可能导致我看到的问题。 使用这种反向字典是很常见的,你也可以在一个好的地方检查isinstance(value, Hashable) 。 你应该没事。

- 编辑 -

你的代码很好,我只是错过了尾随的paren。

Your idea is sound. The dictionary is not regenerated every time, only the first time it is created. Lookup is efficient and reliable, and this is unlikely to cause problems that I can see. Using this kind of reverse dictionary is pretty common, and you have the isinstance(value, Hashable) check in a good place as well. You should be fine.

-- Edited --

Your code is fine, I just missed the trailing paren.

更多推荐

本文发布于:2023-04-27 22:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1328598.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   python   class   lookup   variable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!