[Python] Can I create multiple __name__=="__main__" inside same jupyter notebook?

Discussão em 'Python' iniciado por Stack, Março 6, 2025.

  1. Stack

    Stack Membro Participativo

    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.

    Continue reading...

Compartilhe esta Página