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