在 python 中有异常命中两个 except 块
Have exception hit two except blocks in python
这是我想在代码中做的事情的示例:
class MyClass(object):
@classmethod
def test_func(cls, x):
try:
return cls.cache[x]
except AttributeError:
cls.cache = {}
except (AttributeError, KeyError):
cls.cache[x] = "CACHE STORE"
return cls.cache[x]
这里的想法是我的 class 将根据输入 x
缓存一些结果。但是,在需要之前我不想开始创建缓存。所以我第一次将任何 x
传递给它时,我希望它创建缓存,并用一些东西填充它。有没有办法让它同时击中 except
个方块?现在它目前只命中第一个
我会在这里进行递归:
class MyClass(object):
@classmethod
def test_func(cls, x):
try:
return cls.cache[x]
except AttributeError:
cls.cache = {}
return cls.test_func(x) # Add this line
except KeyError:
cls.cache[x] = "CACHE STORE"
return cls.cache[x]
正如我在评论中建议的那样,在创建 class 时,只需将 cache
设为 class 属性即可。除非您有 确实 不这样做的充分理由,否则您只是在不必要地使您的实施复杂化
class MyClass(object):
cache = {}
@classmethod
def test_func(cls, x):
try:
return cls.cache[x]
except KeyError:
cls.cache[x] = "CACHE STORE"
return cls.cache[x]
一旦你这样做了,你甚至不需要 try
声明;你可以只使用 setdefault
.
@classmethod
def test_func(cls, x):
return cls.cache.setdefault(x, "CACHE STORE")
使用 defaultdict
:
from collections import defaultdict as dd
class MyClass(object):
cache = dd(lambda: "CACHE STORE")
@classmethod
def test_func(cls, x):
return cls.cache[x]
来自文档:
Returns a new dictionary-like object. defaultdict is a subclass of the built-in dict class. It overrides one method and adds one writable instance variable. The remaining functionality is the same as for the dict class and is not documented here.
The first argument provides the initial value for the default_factory attribute; it defaults to None. All remaining arguments are treated the same as if they were passed to the dict constructor, including keyword arguments.
我同意其他人的观点,即从一开始就将其添加为 class 属性不会有任何损失。但是如果你真的坚持在需要之前不创建缓存,你可以这样做:
from collections import defaultdict as dd
class MyClass(object):
@classmethod
def test_func(cls, x):
try:
return cls.cache[x]
except AttributeError:
cls.cache = dd(lambda: "CACHE STORE")
return cls.test_func(x)
这是我想在代码中做的事情的示例:
class MyClass(object):
@classmethod
def test_func(cls, x):
try:
return cls.cache[x]
except AttributeError:
cls.cache = {}
except (AttributeError, KeyError):
cls.cache[x] = "CACHE STORE"
return cls.cache[x]
这里的想法是我的 class 将根据输入 x
缓存一些结果。但是,在需要之前我不想开始创建缓存。所以我第一次将任何 x
传递给它时,我希望它创建缓存,并用一些东西填充它。有没有办法让它同时击中 except
个方块?现在它目前只命中第一个
我会在这里进行递归:
class MyClass(object):
@classmethod
def test_func(cls, x):
try:
return cls.cache[x]
except AttributeError:
cls.cache = {}
return cls.test_func(x) # Add this line
except KeyError:
cls.cache[x] = "CACHE STORE"
return cls.cache[x]
正如我在评论中建议的那样,在创建 class 时,只需将 cache
设为 class 属性即可。除非您有 确实 不这样做的充分理由,否则您只是在不必要地使您的实施复杂化
class MyClass(object):
cache = {}
@classmethod
def test_func(cls, x):
try:
return cls.cache[x]
except KeyError:
cls.cache[x] = "CACHE STORE"
return cls.cache[x]
一旦你这样做了,你甚至不需要 try
声明;你可以只使用 setdefault
.
@classmethod
def test_func(cls, x):
return cls.cache.setdefault(x, "CACHE STORE")
使用 defaultdict
:
from collections import defaultdict as dd
class MyClass(object):
cache = dd(lambda: "CACHE STORE")
@classmethod
def test_func(cls, x):
return cls.cache[x]
来自文档:
Returns a new dictionary-like object. defaultdict is a subclass of the built-in dict class. It overrides one method and adds one writable instance variable. The remaining functionality is the same as for the dict class and is not documented here.
The first argument provides the initial value for the default_factory attribute; it defaults to None. All remaining arguments are treated the same as if they were passed to the dict constructor, including keyword arguments.
我同意其他人的观点,即从一开始就将其添加为 class 属性不会有任何损失。但是如果你真的坚持在需要之前不创建缓存,你可以这样做:
from collections import defaultdict as dd
class MyClass(object):
@classmethod
def test_func(cls, x):
try:
return cls.cache[x]
except AttributeError:
cls.cache = dd(lambda: "CACHE STORE")
return cls.test_func(x)