Why are underscores better than hyphens for file names?

我怕爱的太早我们不能终老 提交于 2019-11-30 04:11:16
Daniel G

The issue here is that importing files with the hyphen-minus (the default keyboard key -; U+002D) in their name doesn't work since it represents minus signs in Python. So, if you had your own module you wanted to import, it shouldn't have a hyphen in its name:

>>> import test-1
  File "<stdin>", line 1
    import test-1
               ^
SyntaxError: invalid syntax
>>> import test_1
>>>

Larger programs tend to be logically separated into many different modules, hence the quote

the name with the hyphen limits our ability to write larger and more sophisticated programs.

From that very document (p.368, Section 30.2 'Module Definition'):

Note that a module name must be a valid Python name... A module's name is limited to letters, digits and "_"s.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!