I'm a little confused about Python's structure. Essentially, when do we need self as a parameter and how should I structure Python projects (not necessarily just a .py file)?
I'm watching an online course that says that the general structure of a class should be as follows:
#!usr/bin/python3.4 # shabang for your python interp. dir.
def main():
print('This is the main')
function1()
def function1():
print('This is function 1')
if __name__ == '__main__':
main()
I am a java programmer, so this is a little awkward for me, but I know that you need the if __name__ == '__main__:
statement so that you can instantiate all of the methods because if you tried to call method1()
from the main without the if statement at the bottom you would get an undefined error.
What I don't understand is where __name__
comes from. Is this created every time you make a new module? Also, why don't you need a method for that if statement. Why can you just put anything anywhere as long as you don't get an undefined error or syntax error??
Also, I remember mention of the teacher saying that for certain functions (I think the __init__()
function as well as generators) you need to pass self into the method as the first parameter. When do we need to pass self in? Do we need to pass it into the main?
Finally, how do you structure a python project? If you want to create a main, how do you make sure the interpreter finds that main and then executes it properly. Does the above if statement take care of that for us?
I know that's a lot of questions, but hanks in advance for the clarification.