Show More
@@ -607,12 +607,66 b' startup files, and everything, just as if it were a normal IPython session.' | |||||
607 | For information on setting configuration options when running IPython from |
|
607 | For information on setting configuration options when running IPython from | |
608 | python, see :ref:`configure_start_ipython`. |
|
608 | python, see :ref:`configure_start_ipython`. | |
609 |
|
609 | |||
610 |
It is also possible to embed an IPython shell in a namespace in your Python |
|
610 | It is also possible to embed an IPython shell in a namespace in your Python | |
611 | This allows you to evaluate dynamically the state of your code, |
|
611 | code. This allows you to evaluate dynamically the state of your code, operate | |
612 |
|
|
612 | with your variables, analyze them, etc. For example, if you run the following | |
613 | any changes you make to values while in the shell do not propagate back |
|
613 | code snippet:: | |
614 | to the running code, so it is safe to modify your values because you |
|
614 | ||
615 | won't break your code in bizarre ways by doing so. |
|
615 | import IPython | |
|
616 | ||||
|
617 | a = 42 | |||
|
618 | IPython.embed() | |||
|
619 | ||||
|
620 | and within the IPython shell, you reassign `a` to `23` to do further testing of | |||
|
621 | some sort, you can then exit:: | |||
|
622 | ||||
|
623 | >>> IPython.embed() | |||
|
624 | Python 3.6.2 (default, Jul 17 2017, 16:44:45) | |||
|
625 | Type 'copyright', 'credits' or 'license' for more information | |||
|
626 | IPython 6.2.0.dev -- An enhanced Interactive Python. Type '?' for help. | |||
|
627 | ||||
|
628 | In [1]: a = 23 | |||
|
629 | ||||
|
630 | In [2]: exit() | |||
|
631 | ||||
|
632 | Once you exit and print `a`, the value 23 will be shown:: | |||
|
633 | ||||
|
634 | ||||
|
635 | In: print(a) | |||
|
636 | 23 | |||
|
637 | ||||
|
638 | It's important to note that the code run in the embedded IPython shell will | |||
|
639 | *not* change the state of your code and variables, **unless** the shell is | |||
|
640 | contained within the global namespace. In the above example, `a` is changed | |||
|
641 | because this is true. | |||
|
642 | ||||
|
643 | To further exemplify this, consider the following example:: | |||
|
644 | ||||
|
645 | import IPython | |||
|
646 | def do(): | |||
|
647 | a = 42 | |||
|
648 | print(a) | |||
|
649 | IPython.embed() | |||
|
650 | print(a) | |||
|
651 | ||||
|
652 | Now if call the function and complete the state changes as we did above, the | |||
|
653 | value `42` will be printed. Again, this is because it's not in the global | |||
|
654 | namespace:: | |||
|
655 | ||||
|
656 | do() | |||
|
657 | ||||
|
658 | Running a file with the above code can lead to the following session:: | |||
|
659 | ||||
|
660 | >>> do() | |||
|
661 | 42 | |||
|
662 | Python 3.6.2 (default, Jul 17 2017, 16:44:45) | |||
|
663 | Type 'copyright', 'credits' or 'license' for more information | |||
|
664 | IPython 6.2.0.dev -- An enhanced Interactive Python. Type '?' for help. | |||
|
665 | ||||
|
666 | In [1]: a = 23 | |||
|
667 | ||||
|
668 | In [2]: exit() | |||
|
669 | 42 | |||
616 |
|
|
670 | ||
617 | .. note:: |
|
671 | .. note:: | |
618 |
|
672 |
General Comments 0
You need to be logged in to leave comments.
Login now