Skip to content
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

Custom namespace for %run #1475

Closed
wants to merge 1 commit into from
Closed

Conversation

mynameisfiber
Copy link

%run now has an optional parameter, -c, to set a custom namespace name.
This parameter is exclusive with -n and should help for special module
instantiation for specific benchmarking or tests.

This is addressing my issue #1474

%run now has an optional parameter, -c, to set a custom namespace name.
This parameter is exclusive with -n and should help for special module
instantiation for specific benchmarking or tests.
@takluyver
Copy link
Member

As mentioned on #1474, I'm -1 on this, because in normal use, __name__ is only ever __main__ or the module name, and I don't think we should encourage using it for anything else.

%run also tries to mimic the python command line arguments (e.g. you can use -m module), so I think using -c is potentially confusing, because Python uses that to pass a statement from the command line.

@ivanov
Copy link
Member

ivanov commented Mar 7, 2012

@mynameisfiber: I'm also having a hard time seeing the usecase for this - it's not clear to me why you'd want to do this from #1474. Can you motivate it better, and explain why this shouldn't live in your own if __name__ == '__main__' code?

@mynameisfiber
Copy link
Author

@ivanov I think an immediate use case for this is if I have code that I want to be able to run as a script, but has completely different setup/teardown necessary for profiling... then I could do something like:

if __name__ == "__main__":
    main()

if __name__ == "__profile__":
    ...do some setup...
    main()
    ...shutdown...

Or the profiling code could do something completely different! Maybe I want to test various configuration methods, or different usage patterns, etc. With all of this, we could simply have various namespace dependent blocks and run them with

%run -c __profile__ -p scripy.py

@takluyver has a good point that -c is being used already in python, but I am fine with any letter (I picked c almost at random!)... the functionality is the most important. Ideally, the -n parameter could have an optional argument, but I couldn't figure out how to do that with getopts!

@takluyver
Copy link
Member

But Python itself offers no option to do this, and I don't think we should encourage people to code in ways specific to IPython. Can't you just use a command line parameter like --profile to achieve the same thing?

@ivanov
Copy link
Member

ivanov commented Mar 9, 2012

@mynameisfiber: seems like the sort of thing you can already do by passing arguments to your code and then checking for them via sys.argv.

@fperez
Copy link
Member

fperez commented Apr 15, 2012

I concur with @ivanov and @takluyver on this, and @takluyver's last comment echoes to a t my perspective. As much as we'd love the whole world to use IPython, IPython remains a Python tool, and something like this which is completely impossible in normal Python should not be done. As @ivanov suggests, you can achieve what you're looking for in a much cleaner manner by using arguments in sys.argv, and you'll have the advantage of a solution that will work both in IPython and outside of it.

I think it's best to close this particular PR. @mynameisfiber, thanks for jumping in IPython development, and we hope this doesn't discourage you from contributing in the future. It's just that in this particular case, there really are better ways to go about the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants