Python中的短路运算(short-circuiting)应该注意哪些问题?

目录

目录

一,前言

二,了解and和or运算符的概念

三,短路运算要注意哪些问题?

顺序问题

类型和值

        整数类型

        字符串类型

        数组类型(以列表为例)

        None与短路运算              

        print()输出函数与短路运算

        与其他运算符一起使用的优先级判定

        优先级顺序表

四,短路运算的副作用和如何避免      

案例一:副作用缺失

案例二:特殊方法__bool__()的实现

 五,总结


一,前言

        在Python中,短路运算(short-circuiting)主要指的是逻辑运算符andor的行为。

        核心思想:当逻辑表达式的值已经能够确定时,Python解释器会停止进一步计算表达式的剩余部分,从而提高效率。这种特性在编写条件判断或控制流语句时非常有用。了解短路运算的行为对于编写高效且正确的代码至关重要。

二,了解and和or运算符的概念

  • 对于and运算符,如果第一个操作数为False,则整个表达式的结果为False,不会评估第二个操作数。
  • 对于or运算符,如果第一个操作数为True,则整个表达式的结果为True,不会评估第二个操作数。
  • 对于not运算符,逻辑非,取反的意思,之前是True,取反后是Flase。  

  

三,短路运算要注意哪些问题?

  • 顺序问题

        由于短路行为,andor运算符中的操作数顺序很重要。如果你希望某个操作数总是被评估(即使它的值不影响最终结果),你需要确保它出现在正确的位置。

        notandor结合使用时,需要特别注意短路行为如何影响整个表达式的评估。例如,not (a and b)并不总是等价于not a or not b,因为前者可能会在aFalse时短路,而后者总是会评估ab

  • 类型和值

        在Python中,不仅FalseTrue会影响短路运算的结果,空值(如None、空列表、空字典等),0在布尔上下文中也会被解释为False。这意味着,如果你使用这些空值作为and运算的第一个操作数,第二个操作数将不会被评估。下面分别举例说明

        整数类型
# 整数类型 
a = 10  
b = 0  
  
# 短路与 and 运算  
result1 = a and b  # a 是 True,但会计算 b,b 是 False,所以结果为 0  
# 短路与 or 运算  
result2 = a or b  # a 是 True,所以不会计算 b,直接返回 a 的值 10 
        字符串类型
# 字符串类型  
c = "Hello"  
d = ""  

# 短路与 and 运算  
result3 = c and d  # c 是 True,但会计算 d,d 在布尔上下文中为 False,所以结果为 ""  
# 短路与 or 运算
result4 = c or d  # c 是 True,所以不会计算 d,直接返回 c 的值 "Hello"
        数组类型(以列表为例)
 # 列表类型  
e = [1, 2, 3]  
f = []  

# 短路与 and 运算  
result5 = e and f  # e 是 True,但会计算 f,f 在布尔上下文中为 False,所以结果为 []  
# 短路与 or 运算 
result6 = e or f  # e 是 True,所以不会计算 f,直接返回 e 的值 [1, 2, 3]
        None与短路运算              
# None与短路运算
x = None  
y = 5  
  
# 短路与 and 运算  
result7 = x and y  # x 是 False,所以不会计算 y,直接返回 x 的值 None   
# 短路与 or 运算  
result8 = x or y  # x 是 False,所以会计算 y,并返回 y 的值 5
        print()输出函数与短路运算

        值得注意的是,print()输出函数的使用是没有返回值的,它的作用是将指定内容打印到控制台,他没有返回值但是可以打印返回值,而且将print函数赋值给变量是None,也就是说直接执行print()会将None打印到控制台上。

# 首先将2打印到控制台并返回None , 第一个and判断返回True ,第二个and判断 打印None
print( 3 > 1 and not None and print(2))    # 输出  2 None 

# 首先进行 2 and 1 返回 1 ,然后先将2打印到控制台 , print(2)返回None和1进行判断 返回1
print(print(2) or 2 and 1)    # 输出 2 1
        与其他运算符一起使用的优先级判定
# and优先级高于or , 首先2 and 3返回 3 , 然后1 or 3返回1
print(1 or 2 and 3)  # 1   

# 比较符 > not > and > or ,
print(3 > 1 or not 3 and 5 < 4)  # True
        优先级顺序表

        这是本人自己总结的优先级判定,不够完整请自行补充。

四,短路运算的副作用和如何避免      

  • 案例一:副作用缺失

        当使用函数或方法作为andor运算符的操作数时,需要特别注意。由于短路行为,某些函数可能不会被调用,这可能导致预期之外的副作用缺失。

# 案例一:
def log_message(message):  
    print(f"Logging: {message}")  
    return True  
  
# 假设某个条件始终为False  
condition = False  
  
# 由于短路,log_message不会被调用  
if condition and log_message("This will not be logged"):  
    pass

解决措施:文档和测试
        当使用短路运算时,确保你的代码有充分的文档说明,并且通过了适当的测试。这有助于其他开发者理解你的代码,并确保它在各种情况下都能正常工作,特别要注意那些可能触发短路行为的边界条件。

  • 案例二:特殊方法__bool__()的实现

        自定义对象在布尔上下文中的行为取决于其是否实现了特殊方法__bool__()。如果没有实现,并且对象不是None、数值零、空序列或空映射等被认为是False的值,则对象在布尔上下文中被视为True。

class MyClass:  
    def __init__(self, value):  
        self.value = value  
  
    # 假设我们没有实现 __bool__ 方法  
  
obj1 = MyClass(True)  
obj2 = MyClass(False)  
  
# 短路与 and 运算 
# obj1 被视为 True,会计算 obj2,但 obj2 的类型不影响结果,
# 所以返回 obj2 对象本身   
result7 = obj1 and obj2  
  
# 短路与 or 运算
# obj2 被视为 True(因为没有实现 __bool__ 且不是Falsey值),
# 所以不会计算 obj1,直接返回 obj2 对象本身  
result8 = obj2 or obj1  

解决措施:清晰性与可读性

        虽然短路运算可以提高代码效率,但过度使用或在不适当的地方使用可能会降低代码的可读性和可维护性。为了保持代码的清晰性,建议:

  • 当逻辑表达式简单且没有副作用时,可以安全地使用短路运算。
  • 当逻辑表达式复杂或涉及到函数调用时,应尽量避免使用短路运算,或者至少确保代码的可读性,比如通过添加注释来解释短路行为。
  • 如果可能的话,尽量将复杂的逻辑判断拆分成多个简单的判断,以提高代码的可读性。

 

 五,总结

        综上所述,Python中的短路运算虽然强大且有用,但也需要谨慎使用。通过理解其工作原理、注意操作数的顺序和可能的副作用、保持代码的清晰性和可读性,并进行充分的测试和调试,可以确保短路运算在Python代码中的正确使用。

  • 49
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Abstract: Gas metal arc welding (GMAW) is a widely used welding process in various industries. One of the significant challenges in GMAW is to achieve optimal welding parameters and minimize defects such as spatter and porosity. In this paper, we propose a deep-learning-based approach to analyze metal-transfer images in GMAW processes. Our approach can automatically detect and classify the different types of metal-transfer modes and provide insights for process optimization. Introduction: Gas metal arc welding (GMAW) is a welding process that uses a consumable electrode and an external shielding gas to protect the weld pool from atmospheric contamination. During the GMAW process, the metal transfer mode affects the weld quality and productivity. Three types of metal transfer modes are commonly observed in GMAW: short-circuiting transfer (SCT), globular transfer (GT), and spray transfer (ST). The selection of the transfer mode depends on the welding parameters, such as the welding current, voltage, and wire feed speed. The metal transfer mode can be observed using high-speed imaging techniques, which capture the dynamic behavior of the molten metal during welding. The interpretation of these images requires expertise and is time-consuming. To address these issues, we propose a deep-learning-based approach to analyze metal-transfer images in GMAW processes. Methodology: We collected a dataset of metal-transfer images using a high-speed camera during the GMAW process. The images were captured at a rate of 5000 frames per second, and the dataset includes 1000 images for each transfer mode. We split the dataset into training, validation, and testing sets, with a ratio of 70:15:15. We trained a convolutional neural network (CNN) to classify the metal-transfer mode from the images. We used the ResNet50 architecture with transfer learning, which is a widely used and effective approach for image classification tasks. The model was trained using the categorical cross-entropy loss function and the Adam optimizer. Results: We achieved an accuracy of 96.7% on the testing set using our deep-learning-based approach. Our approach can accurately detect and classify the different types of metal-transfer modes in GMAW processes. Furthermore, we used the Grad-CAM technique to visualize the important regions of the images that contributed to the classification decision. Conclusion: In this paper, we proposed a deep-learning-based approach to analyze metal-transfer images in GMAW processes. Our approach can automatically detect and classify the different types of metal-transfer modes with high accuracy. The proposed approach can provide insights for process optimization and reduce the need for human expertise in interpreting high-speed images. Future work includes investigating the use of our approach in real-time monitoring of the GMAW process and exploring the application of our approach in other welding processes.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值