-
-
Notifications
You must be signed in to change notification settings - Fork 807
Rename python files in code not to have hyphens #254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rename python files in code not to have hyphens #254
Conversation
|
The naming using dashes instead of underscores is intentional to prevent the module from being imported. Python does not make any difference between modules (just To prevent code from being imported you can change the PEP8-recommended The reason you may want certain files to not being "importable" is because there should be nothing useful to import in your scripts. See, for example, one of the scripts you are renaming: This code is meant to be executed, not imported and, in fact, fails when you import it. In other cases it could be even worse, the code in So, in general, importing a module should not execute code and therefore the I learned this from @ctb, so maybe he wants to add some comments or references. |
|
If we're worried about importing, why not a little block like: if __name__ != '__main__':
raise ImportError("Don't import this file; run it from the shell instead.")at the top of the file? |
|
@tbekolay, the - is a visual convention that prevents people from thinking that scripts are modules that can be imported and then trying to import them. If there are scripts that should never be imported, it should be fine to name them with a - in there, right? This is a convention I've seen elsewhere in the Python world, but google doesn't do a good job of finding '-' ;). (@iglpdc, in general I don't think we should be teaching 'from gen_inflammation import *' anyway, which is the only way namespaces would be polluted.) |
|
Hm... this is a convention that I've never heard before, so I would be interested to see examples of it; the only things I can find through googling is people asking how to import python scripts with hyphens (which is possible with |
@ctb Sure! The main point is that importing modules that execute code may side effects and this is not what people expects. @tbekolay The standard way would be to enclose your script code into a I think that having scripts with really long names is characteristic of scientific settings and maybe that's why it's no many Python references around. |
|
@iglpdc I think the As I guess you all know, the most pythonic whay would be to embed scripts like: def main():
pass # actual script here
if __name__ == '__main__':
main()but the question is, do we want to teach that to students or not? Maybe it is good to mention somewhere that importing a module executes the code? That is a valid warning to give when teaching students about import statements. I think the hyphen is a poor substitute for the proper construct, so I would vote to either let it be importable (even though there is no reason to do so and it can have strange effects) or to use a proper way avoid it from accidental execution on import. |
|
I would say that very few novice workshops get to this topic anyhow, so I'm not too concerned about one line being a little cryptic if we have a callout box explaining it. In any case, this is more contentious than I was expecting, so it probably warrants some additional comments. Looking back at the topic itself, most of the files already factor the code into a |
That's true. We could use underscores, main functions in the readings scripts and a call-out box "Why the In any case, I think it's more important to be truthful to the language ( |
|
Re dashes vs underscores vs..., Twitter conversation here -- https://twitter.com/ctitusbrown/status/713365613113028608 The most interesting bits are:
Anyway, I guess that's a -1 on teaching it as best practice... |
|
I've merged this now, and added commits to add |
Clarifying 2x2 plot (grid)
Renames the hyphen to underscores as #152 and #172 suggest. I am not sure if I caught all the references to the files, but I used grepping the .md and code/.py to check for references.