0

I have a question when it comes to OOP in general, as well as Python in particular. Let's say that I have, for instance, priorities.py - a simple GUI program to manage priorities and there are three classes: Priority, Client, GuiPart:

# priorities.py
#   GUI program to manage priorities

from tkinter import *

class Priority:
    pass

class GuiPart:
    def __init__(self):
        self.root = self.createWindow()

    def createWindow(self):

        root = Tk()
        root.resizable(width = False, height = False)
        root.title("Priorities")

        return root

    def display(self):

        Label(self.root,
              text = "testes").grid(row = 0, column = 1)

class Client:
    pass

def main():
    g = GuiPart()
    g.display()
    root = g.root.mainloop()

main()

Should I put def main() outside of any classes, or should I put it in Client class?

wraith46
  • 305
  • 1
  • 6
  • 17
  • 2
    I'm not sure what the style guide says (or what other people think), but personally I don't use a `def main()`, I use an `if __name__ == '__main__:` and put it at the bottom of the file outside of any classes (like you are doing). – gen_Eric Mar 31 '17 at 14:01
  • Unless your code doesn't work or you want to improve its efficiency, this question is better suited for Code Review. – Eli Korvigo Mar 31 '17 at 14:01
  • 1
    @RocketHazmat Defining `main` as a function as well makes it easier to test that code. – chepner Mar 31 '17 at 14:12
  • 2
    @RocketHazmat `def main():` and `if __name__ == '__main__:` are not mutually exclusive. `if __name__ == '__main__: main()` is common and as @chepner said makes testing `main` elsewhere easy. – BallpointBen Mar 31 '17 at 14:15
  • Fair enough! I'm just learning python and that's how *I* do it. Maybe I need to fix how I do things :) – gen_Eric Mar 31 '17 at 14:16
  • @BallpointBen I am not sure that I completely understand "mutually exclusive". Could you describe it in more words? – wraith46 Mar 31 '17 at 14:19
  • 1
    @wraith46 Rocket Hazmat said he doesn't use a `main()`, he uses an `if __name__`. I'm saying that it's not an either or; you can put all `main` functionality in one function and then call that function from `if __name__` – BallpointBen Mar 31 '17 at 15:29
  • Thanks @BallpointBen – wraith46 Mar 31 '17 at 15:37

2 Answers2

4

Every module(python file) have a builtin __name__ variable, if this equal to "__main__" this means that this file ran directly, but if __name__ is equal to other things this means that current file imported to other python files.

if you running this file directly or as module, you can use __name__ variable to recognize type of code-file used, similar below:

# Some codes
if __name__ == '__main__':
    main()

Now users can running this file directly and/or programmers can use this module in other codes without running main() function.

M.javid
  • 6,387
  • 3
  • 41
  • 56
2

My preferred approach:

separate main file with the if __name__ == '__main__': directive

Reasons:

  1. Application Logic and calling logic is separate. so you can scale easily
  2. Can maintain and apply different environment settings effectively. so, we can seamlessly transition between dev/test/stage/prod setup
  3. Increases code readability as well
timgeb
  • 76,762
  • 20
  • 123
  • 145
Priyank Mehta
  • 2,453
  • 2
  • 21
  • 32
  • I am sorry but I don't completely understand, __name__ is name of what? Of a .py file? – wraith46 Mar 31 '17 at 14:10
  • 1
    its a magic word in python.. this will reflect the file name and hence when you directly run the py file ex.. (xyz.py) it will understand this is the starting calling point and hence whatever is written within this block will be executed.. rest of time it will not.. more on this http://stackoverflow.com/questions/419163/what-does-if-name-main-do – Priyank Mehta Mar 31 '17 at 14:12
  • 1
    Put another way, it's a way to specify code that should only run when the script is executed as a script, but not when you import it as a module from another script (or another module). You may not expect your script to be imported, but that's how most (if not all) test frameworks gain access to your code to run tests. – chepner Mar 31 '17 at 14:18
  • @wraith46 You're welcome. You can mark the answer as accepted, if you feel it has cleared your doubts. – Priyank Mehta Mar 31 '17 at 14:20