在Jupyter笔记本中使单元彼此独立 [英] Making cells independent of each other in a Jupyter notebook

查看:154
本文介绍了在Jupyter笔记本中使单元彼此独立的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想进行一些独立的计算,所有这些计算都在jupyter笔记本的自己的单元格中-如果愿意,每个单元格都具有自己的主要"功能.目前看来,所有包含Python的单元的并集 代码本质上是一个大型Python程序.

I would like to have a few independent computations, all them in their own cells in a jupyter notebook -- each cell having its own "main" function if you will. Currently it looks like the union of all cells containing Python code is essentially one big Python program.

简而言之,我要问Jupyter版本的

In brief I am asking a Jupyter version of this question for Mathematica.

推荐答案

在单元格中定义的变量成为全局命名空间中的变量. 要将变量隔离到本地范围,请将其放入函数中:

Variables defined in cells become variables in the global namespace. To isolate variables to a local scope, put them in functions:

In [1]: 

    def foo():
        x = 1
        return x
    foo()

In [2]: 

    def bar():
        x = 2
        return x
    bar()

这篇关于在Jupyter笔记本中使单元彼此独立的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆