开发者

Can Python directory names be keywords? E.g. 'import'?

Am I allowed to have a director开发者_Python百科y named 'import' containing Python code? Or will the import command fail to parse it as a result? Is there any way around that?


You can use the built-in __import__ function which accepts any string. Thus you may write:

__import__('keyword.submodule')


You can have a directory with a name that is a Python keyword storing your Python code. This directory should not be used as a package, since package names should be valid Python identifiers.


Or will the import command fail to parse it as a result?

It will indeed fail.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜