开发者

Python equivalent of the ! operator

开发者 https://www.devze.com 2023-03-25 23:00 出处:网络
For the following construct what can the more pythonic way? If it were C 开发者_Python百科I could just use ! but what is it equivalent in Python?

For the following construct what can the more pythonic way?

If it were C 开发者_Python百科I could just use ! but what is it equivalent in Python?

file_path = # something 
if os.path.exists(file_path):
     pass
else:
  file_path = # make new path 


file_path = # something 
if not os.path.exists(file_path):
     file_path = #make new path

Python Docs - Expressions - 5.1 Boolean Expressions


The not keyword.

if not os.path.exists(file_path):
    file_path ...


Simply:

file_path = # something 
if not os.path.exists(file_path):
    file_path = # make new path 

Operator not is also in C++, btw, as a synonym for !.

0

精彩评论

暂无评论...
验证码 换一张
取 消