开发者

Detecting the presence of IDLE / How to tell if __file__ isn't set

开发者 https://www.devze.com 2023-03-28 01:47 出处:网络
I had a script that need开发者_C百科ed the use of __file__, and so I learned that IDLE doesn\'t set this. Is there a way from my script that I can detect the presence of IDLE? if \'__file__\' not in g

I had a script that need开发者_C百科ed the use of __file__, and so I learned that IDLE doesn't set this. Is there a way from my script that I can detect the presence of IDLE?


if '__file__' not in globals():
    # __file__ is not set

if you want to do something special if __file__ isn't set. Or,

try:
    __file__
except NameError:
    # __file__ is not set
    raise

if you want to do something then raise an error anyway, or

global __file__
__file__ = globals().get('__file__', 'your_default_here')

if you want to have a default.

0

精彩评论

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

关注公众号