跳过下一次迭代,如果出现警告。

huangapple go评论50阅读模式
英文:

Skip to the next iteration if a warning is raised

问题

如何在出现警告时跳过迭代

假设我有以下代码:

import warnings

# 可能会引发警告的函数
def my_func(x):
    if x % 2 != 0:
        warnings.warn("This is a warning")
        return "Problem"
    else:
        return "No Problem"

for i in range(10):
    try:
        # 可能引发警告的代码
        k = my_func(i)
    except Warning:
        # 如果引发警告,则跳过到下一个迭代
        continue

    # 余下的代码
    print(i, " : ", k) # 仅在 try: except 中未引发警告时才打印此内容

我期望这只会打印偶数,因为 my_func(i) 会对奇数引发警告。

更新:

my_func(i) 仅用于说明目的,我想要使用的实际问题可能没有明显的返回值来引发警告。

英文:

How can I skip the iteration if warning is raised

Suppose I have the code below

import warnings

# The function that might raise a warning
def my_func(x):
    if x % 2 != 0:
        warnings.warn("This is a warning")
        return "Problem"     
    else:
        return "No Problem"
        

for i in range(10):
    try:
        # code that may raise a warning
        k = my_func(i)
    except Warning:
        # skip to the next iteration if a warning is raised
        continue
        
    # rest of the code
    print(i, " : ",k) # Only print this if warning was not raised in try:except

I would expect this to print only even numbers as my_funct(i) will raise a warning for odd numbers

update:

my_func(i) was used just for illustration purposes, the actual problem I want to use might not have an obvious returned value that raises a warning.

答案1

得分: 2

Warnings don't throw an exception by default.
You can specify warnings to throw an exception.
Just run this right after the import

warnings.simplefilter("error")

Or, you can just check the result of the function. Check if the result was "Problem" or "No Problem".

for i in range(10):
    k = my_func(i)
    if k == "Problem":
        continue
        
    # rest of the code
    print(i, " : ", k)
英文:

Warnings don't throw an exception by default.
You can specify warnings to throw an exception.
Just run this right after the import

warnings.simplefilter("error")

Or, you can just check the result of the function. Check if the result was "Problem" or "No Problem".

for i in range(10):
    k = my_func(i)
    if k == "Problem":
        continue
        
    # rest of the code
    print(i, " : ",k)

答案2

得分: 1

不要使用 warn 函数,而是抛出一个警告对象,除非检测到异常。

代码

import warnings

# 可能会触发警告的函数
def my_func(x):
    if x % 2 != 0:
        raise Warning('This is a warning')
    else:
        return "No Problem"

for i in range(10):
    try:
        # 可能会触发警告的代码
        k = my_func(i)
    except Warning:
        continue
    print(i, " : ", k)

输出

0 : No Problem
2 : No Problem
4 : No Problem
6 : No Problem
8 : No Problem
英文:

Instead of using warn function throw a warning object that except will detect.

CODE:

import warnings

# The function that might raise a warning
def my_func(x):
    if x % 2 != 0:
        raise Warning('This is a warming')
    else:
        return "No Problem"
    

for i in range(10):
    try:
        # code that may raise a warning
        k = my_func(i)
    except Warning:
        continue
    print(i, " : ",k) 

OUTPUT:

0 : No Problem
2 : No Problem
4 : No Problem
6 : No Problem
8 : No Problem

huangapple
  • 本文由 发表于 2023年2月14日 00:12:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438450.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定