我可以创建多个__NAME __ =='__ Main __;在同一Jupyter笔记本中?

发布于 2025-01-22 22:56:41 字数 1170 浏览 0 评论 0原文

我有一个.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还在原地等你 2025-01-29 22:56:41

嗯,这有点奇怪。

您有一个正确的想法,想让事情分开。
只是做一点不同,
使用功能定义来实现您的目标:

def main1():
    ...

def main2():
    ...

def main3():
    ...

main1()
main2()
main3()

背景:为什么我们完全使用__ MAIM __ IDIOM?

好吧,它起源于Jupyter Notebook出现在现场之前。

def foo()上工作的开发人员:
会在测试期间运行它,
并且通常会使foo.py的最后一行看起来像:

if __name__ == '__main__':
    print('result was', foo())

现在,我们可以创建foo_test.py.py file,
但是通常以上是最方便的。

为什么不只是foo()的单行调用?
我们通过其他模块保护呼吁导入
考虑附近的baz.py源文件:

from foo import foo

def baz():
    return 2 * foo()

我们不希望该初始导入行
吵闹或有其他副作用
就像更改文件或服务器下降时失败一样。
如果保护使其他模块可靠地导入,
只有“无聊”的事物的知识安全
像班级和功能定义一样。

在笔记本中,我们很少遇到此类考虑。
因此,简单地调用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:

def main1():
    ...

def main2():
    ...

def main3():
    ...

main1()
main2()
main3()

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:

if __name__ == '__main__':
    print('result was', foo())

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:

from foo import foo

def baz():
    return 2 * foo()

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 an if, makes sense.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文