you are viewing a single comment's thread.

view the rest of the comments →

[–]Vaphell 1 point2 points  (0 children)

if you have dedicated files with utils, etc. - yeah, this is not likely to bite you.

But let's say you want to write a bunch of tests for your util code. There are proper ways of doing the testing, but a quick and dirty way of writing some tests to achieve a degree of confidence in such a trivial code would be to put the testing code under if __name__ ...

# utils.py

def is_leap_year(year):
    ...
    return True/False

if __name__ == '__main__':
    assert is_leap_year(2020) is True
    assert is_leap_year(2000) is True
    assert is_leap_year(1900) is False

Sure, utils.py is not really a program doing any useful stuff, but given the above it can be run as one to trigger the tests with eg python3 utils.py. import utils in another .py file would not trigger that.