python让函数抛出异常,是否有任何对象可以使str()函数在python中抛出错误或异常?...
我有一個函數,要求輸入是一個字符串.
我知道我可以斷言或檢查輸入類型,但我想盡可能地處理它.
我有以下代碼來處理它.但我想知道是否有任何情況,這一行可以拋出我需要處理的異常.
def foo(any_input):
clean_input = str(any_input) # will this throw any exception or error?
process(clean_input)
嘗試str深度嵌套列表時,可能會遇到RuntimeError:
>>> x = []
>>> for i in range(100000):
... x = [x]
...
>>> y = str(x)
Traceback (most recent call last):
File "", line 1, in
RuntimeError: maximum recursion depth exceeded while getting the repr of a list
或者一個MemoryError試圖查找一個巨大的列表:
>>> x = 'a'*1000000
>>> y = [x] * 1000000 # x and y only require a few MB of memory
>>> str(y) # but str(y) requires about a TB of memory
Traceback (most recent call last):
File "", line 1, in
MemoryError
總結
以上是生活随笔為你收集整理的python让函数抛出异常,是否有任何对象可以使str()函数在python中抛出错误或异常?...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 青柿子泡酒的功效与作用、禁忌和食用方法
- 下一篇: 土鸡蛋的功效与作用、禁忌和食用方法