Class and method: main() in python

Class and method: main() in python

I have seen examples in python where they use class main() or method main(). I wonder if it has the same meaning as in Java, where the main method gets executed first, or it is just a name that can be different than main() ?

Thank you
jskfanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gelonidaCommented:
I never saw an example with a main() class.
Perhaps you could share this example with us?

The main idea is, that a python file could be used in two different ways.

Let's assume for example, that your script is called myscript.py:

It could either be called as a script with
python myscript.py

Open in new window

or if the script has been made executable just from command line with
./myscript.py  # non windows
myscript.py # windows

Open in new window


or it could be imported  by another python script / python file with
following line from within another python file
import myscript

Open in new window



If you want, that your python file can be imported as a module and provide some functionality, that can be used
by somebody, but your script shall also be callable as a script, then there's some code, that can achieve this

let's look assume myscript.py contains following code:

def add_three_vals(a, b, c):
    return a + b + c

def main():
    values = [
        (1, 2, 3),
        (4, 5, 1),
    ]

    for v1, v2, v3 in values:
        print("%d + %d + %d = %d", v1, v2, v3, add_three_vals(v1, v2, v3))

# the next two lines will make sure, that main() is only called if the python file
# is executed as script
if __name__ == "__main__":
    main()




Some people don't even declare the main function.

They just do following at the end of the file

if __name__ == "__main__":
    values = [
        (1, 2, 3),
        (4, 5, 1),
    ]

    for v1, v2, v3 in values:
        print("%d + %d + %d = %d", v1, v2, v3, add_three_vals(v1, v2, v3))

# the next two lines will make sure, that main() is only called if the python file
# is executed as script

Open in new window



The name of the function main() is completely arbitrary it could have any other name.

However the if statement will always be

if __name__ == "__main__":

Open in new window


__name__ is a special variable, which will have the value "__main__" if the script is called or the name of the module (in our above example that would be "myscript") if the file has been imported
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jskfanAuthor Commented:
Ok............ I have seen Main used in Java , which is the starting point of the code compiling I believe.

Python has __init__(self)

I wonder if the word init can be changed , for instance:

__Test__(self)
0
jskfanAuthor Commented:
after reading online,
it says __init__(self) will be executed without calling the function from within the code.

I got this one
0
jskfanAuthor Commented:
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.