我可以创建多个__NAME __ =='__ Main __;在同一Jupyter笔记本中?
我有一个.ipynb
jupyter笔记本文件,我在哪里工作。 该文件包含多个类和方法。其中一些是依赖的,而其中一些不是。例如:
class Node:
## this is where I create a single node
class LinkedList:
## this is where I create linked list
之后,我创建了另一个类,其中继承了基础类并实现算法。
class Solution:
## this is where I implement my algorithm
method1():
# method1 implementation
method2():
# method2 implementation
最后,我在下面的不同单元格中创建多个主电源。
## main1 -- test case #1
if __name__ == "__main__":
## declare variables
## call classes and methods
## print something
在下一个单元格中,我会执行以下操作。
## main2 -- test case #2
if __name__ == "__main__":
## declare variables
## call classes and methods
## print something
同样,我会在下一个单元格中这样做。
## main3 -- test case #3
if __name__ == "__main__":
## declare variables
## call classes and methods
## print something
我喜欢本地解决leetcode问题时,我喜欢这个模板。但是我认为在调试时,这有点令人困惑。
在Jupyter笔记本工作环境中这样做是一种标准做法吗?它会弄乱范围吗?请帮助我了解这是否是一个好练习,还是向我推荐如何在同一Jupyter笔记本中运行多个测试用例。 非常感谢您阅读这篇文章。
I have a .ipynb
jupyter notebook file, where I am working.
The file contains multiple classes and methods. Some of them are dependent while some of them are not. For example:
class Node:
## this is where I create a single node
class LinkedList:
## this is where I create linked list
After this, I create another class where I inherit my base classes and implement algorithms.
class Solution:
## this is where I implement my algorithm
method1():
# method1 implementation
method2():
# method2 implementation
Finally, I create multiple mains in different cells below.
## main1 -- test case #1
if __name__ == "__main__":
## declare variables
## call classes and methods
## print something
In the next cell, I would do the following.
## main2 -- test case #2
if __name__ == "__main__":
## declare variables
## call classes and methods
## print something
Similarly, I would do this in next cell.
## main3 -- test case #3
if __name__ == "__main__":
## declare variables
## call classes and methods
## print something
I like this template while solving leetcode problems locally. But I think while debugging, it's a little bit confusing.
Is it a standard practice to do so in the jupyter notebook working environment? Does it mess up with scopes? Please help me understand if it a good practice, or recommend me how to run multiple test cases within the same jupyter notebook.
Thank you so much for reading this far.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,这有点奇怪。
您有一个正确的想法,想让事情分开。
只是做一点不同,
使用功能定义来实现您的目标:
背景:为什么我们完全使用
__ MAIM __
IDIOM?好吧,它起源于Jupyter Notebook出现在现场之前。
在
def foo()上工作的开发人员:
会在测试期间运行它,
并且通常会使
foo.py
的最后一行看起来像:现在,我们可以创建
foo_test.py.py
file,但是通常以上是最方便的。
为什么不只是
foo()
的单行调用?我们通过其他模块保护呼吁
导入
。考虑附近的
baz.py
源文件:我们不希望该初始导入行
吵闹或有其他副作用
就像更改文件或服务器下降时失败一样。
如果
保护使其他模块可靠地导入,只有“无聊”的事物的知识安全
像班级和功能定义一样。
在笔记本中,我们很少遇到此类考虑。
因此,简单地调用
main1()
,没有,如果,则是有道理的。Ummm, that's a little weird.
You have the right idea, wanting to keep things separate.
Just do it a bit differently,
use function definitions to accomplish your goal:
Background: why do we use that
__main__
idiom at all?Well, it originated long before Jupyter notebooks appeared on the scene.
A developer working on
def foo():
will want to run it during testing,
and will commonly make the last lines of
foo.py
look like:Now, we could create a
foo_test.py
file,but often the above will be most convenient.
Why not just a single-line call of
foo()
?We protect the call for safe
import
by other modules.Consider the nearby
baz.py
source file:We wouldn't want that initial import line
to be noisy, or have other side effects
like changing a file or failing when a server's down.
The
if
protection lets other modules reliably import,safe in the knowledge that only "boring" things
like class and function definitions will be happening.
In a notebook, we seldom encounter such considerations.
So a simple call to
main1()
, without anif
, makes sense.