Show More
@@ -609,10 +609,76 b' python, see :ref:`configure_start_ipython`.' | |||||
609 |
|
609 | |||
610 | It is also possible to embed an IPython shell in a namespace in your Python code. |
|
610 | It is also possible to embed an IPython shell in a namespace in your Python code. | |
611 | This allows you to evaluate dynamically the state of your code, |
|
611 | This allows you to evaluate dynamically the state of your code, | |
612 |
operate with your variables, analyze them, etc. |
|
612 | operate with your variables, analyze them, etc. For example, if you run the | |
613 | any changes you make to values while in the shell do not propagate back |
|
613 | following 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 | ``` python | |
|
616 | import IPython | |||
|
617 | ||||
|
618 | a = 42 | |||
|
619 | IPython.embed() | |||
|
620 | ``` | |||
|
621 | ||||
|
622 | and within the IPython shell, you reassign `a` to `23` to do further testing of | |||
|
623 | some sort, you can then exit. | |||
|
624 | ||||
|
625 | ``` | |||
|
626 | >>> IPython.embed() | |||
|
627 | Python 3.6.2 (default, Jul 17 2017, 16:44:45) | |||
|
628 | Type 'copyright', 'credits' or 'license' for more information | |||
|
629 | IPython 6.2.0.dev -- An enhanced Interactive Python. Type '?' for help. | |||
|
630 | ||||
|
631 | In [1]: a = 23 | |||
|
632 | ||||
|
633 | In [2]: exit() | |||
|
634 | ``` | |||
|
635 | ||||
|
636 | Once you exit and print `a`, the value 23 will be returned: | |||
|
637 | ||||
|
638 | ||||
|
639 | ``` python | |||
|
640 | print(a) | |||
|
641 | ``` | |||
|
642 | ``` | |||
|
643 | 23 | |||
|
644 | ``` | |||
|
645 | ||||
|
646 | It's important to note that the code run in the embedded IPython shell will | |||
|
647 | *not* change the state of your code and variables, **unless** the shell is | |||
|
648 | contained within the global namespace. In the above example, `a` is changed | |||
|
649 | because this is true. | |||
|
650 | ||||
|
651 | To further exemplify this, consider the following example: | |||
|
652 | ||||
|
653 | ``` python | |||
|
654 | import IPython | |||
|
655 | def do(): | |||
|
656 | a = 42 | |||
|
657 | print(a) | |||
|
658 | IPython.embed() | |||
|
659 | print(a) | |||
|
660 | ``` | |||
|
661 | ||||
|
662 | Now if call the function and complete the state changes as we did above, the | |||
|
663 | value `42` will be returned. Again, this is because it's not in the global namespace. | |||
|
664 | ||||
|
665 | ``` python | |||
|
666 | do() | |||
|
667 | ``` | |||
|
668 | ``` | |||
|
669 | 42 | |||
|
670 | >>> IPython.embed() | |||
|
671 | Python 3.6.2 (default, Jul 17 2017, 16:44:45) | |||
|
672 | Type 'copyright', 'credits' or 'license' for more information | |||
|
673 | IPython 6.2.0.dev -- An enhanced Interactive Python. Type '?' for help. | |||
|
674 | ||||
|
675 | In [1]: a = 23 | |||
|
676 | ||||
|
677 | In [2]: exit() | |||
|
678 | ``` | |||
|
679 | ``` | |||
|
680 | 42 | |||
|
681 | ``` | |||
616 |
|
682 | |||
617 | .. note:: |
|
683 | .. note:: | |
618 |
|
684 |
General Comments 0
You need to be logged in to leave comments.
Login now