Should I create each class in its own .py file?

前端 未结 6 1396
故里飘歌
故里飘歌 2020-12-18 18:01

I\'m quite new to Python in general.

I\'m aware that I can create multiple classes in the same .py file, but I\'m wondering if I should create each class in its own

6条回答
  •  天涯浪人
    2020-12-18 18:26

    Yes each class in its own file. Importing even a single class (or function) in a file with multiple classes causes python to execute the definition of all classes in the file. Try this:

    manyClass.py

    class foo():
       print 'a bunch of time consuming work'
    
    class tryme():
       print 'try me'
    

    Now type this in the interpreter shell...

    from manyClasses import tryme

    a bunch of time consuming work
    try me

提交回复
热议问题