0

In R, I very frequently set session options to control program flow. For example, if I want my script to run in debug mode i can do options(debug = T) and then check for it in my script with getOption("debug", F). Does python have anything equivalent?

Note this is not the same as passing an argument to my script as I want to not touch the code after starting debugging.

Alex
  • 1,281
  • 1
  • 13
  • 26
  • Have you looked for answers to this question? What did you find? For example, [here](https://stackoverflow.com/questions/16867347/step-by-step-debugging-with-ipython) is some information all about debugging with IPython. – Bill Jul 07 '18 at 20:41
  • yes - i have found nothing other than setting environment variables. also, this question has nothing to do with debugging, that was simply an example of where this mechanism is useful – Alex Jul 07 '18 at 20:47
  • Oh okay. When I googled 'ipython options' I got this result: [Docs » Configuration and customization » IPython options](https://ipython.readthedocs.io/en/stable/config/options/index.html?highlight=options) It contains a list of the options available in different types of IPython environments. There are also [magic commands](https://ipython.readthedocs.io/en/stable/interactive/magics.html) Do any of these look like the options you are looking for? If not what other types of options are you looking for? – Bill Jul 07 '18 at 20:55
  • yes, i've seen this as well. i'm looking to set a custom defined option. `debug` is not a built in option into R, it is like an environment variable that is local to that specific R session. – Alex Jul 07 '18 at 20:57
  • 1
    Sounds like you mean user-defined settings for your script. Check out [`argparse`](https://docs.python.org/3/library/argparse.html). That's all I can think of. Or you could simply pass a variable as a function argument e.g. `def myfunc(debug=False)` and then call it with `myfunc(debug=True)` when you need that. – Bill Jul 07 '18 at 21:05
  • One thing about Python I think is that most things are explicit. As far as I'm aware you can't (or shouldn't) write a script that has behaviour dependent on some invisible environment setting. – Bill Jul 07 '18 at 21:09
  • Also, you might be interested in [`logging`](https://docs.python.org/3/library/logging.html) – Bill Jul 07 '18 at 21:12
  • ok sounds good, i'll check that out. thanks for your help. – Alex Jul 07 '18 at 21:25
  • Always happy to help an R convert. Feel free to upvote any comments that were helpful... – Bill Jul 07 '18 at 21:26

0 Answers0