1

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.

dylnmc
  • 3,810
  • 4
  • 26
  • 42
  • possible duplicate of [Python 'self' explained](http://stackoverflow.com/questions/2709821/python-self-explained) – g.d.d.c Jul 22 '14 at 19:42
  • This [Post](http://stackoverflow.com/questions/419163/what-does-if-name-main-do) should help with a lot of your questions. – Brian S Jul 22 '14 at 19:43
  • 3
    These are the very basics of Python. Please look into the first book you can find e.g. [this one](http://www.diveintopython.net/) – Adam Kosiorek Jul 22 '14 at 19:43
  • 2
    You should aquatint yourself with the differences between classes and modules in Python. Unlike Java, not everything is a class (at least an explicit class) in Python, but your question seems to be conflating the terms. – Silas Ray Jul 22 '14 at 19:43
  • With all our willingness to help, this is too much to answer in a single post. I hope you'll get enough pointers in the comments to get you started, though. – Lev Levitsky Jul 22 '14 at 19:44
  • @SilasRay Isn't everything in Python an object? And, yes, not everything is a class. – octopusgrabbus Jul 22 '14 at 19:46
  • Shame this was downvoted. I think it's an interesting question. – octopusgrabbus Jul 22 '14 at 19:47
  • Also, FYI, `main()` is perfectly well defined by the point you call it, all `if __name__ == '__main__'` does is make it so the code in that block only executes if run in the `__main__` module, aka as a script from the terminal. That code will not run if you were to import the module in to another module. – Silas Ray Jul 22 '14 at 19:47
  • @octopusgrabbus True, typed that a bit fast. Also hard to get all the detail in a comment. :) Everything is an object, but some objects (like, for instance, modules) are implicitly created, not explicitly defined with `class`. – Silas Ray Jul 22 '14 at 19:49
  • @user3865473 In the future, you should look to ask your questions separately -- separately, it's likely that all of these questions have been answered elsewhere. – forivall Jul 22 '14 at 20:19

2 Answers2

0

Your instruction is not wrong, but may be confusing to your understanding of python's structure. Python calls each file a "module" not a "class", and I will demonstrate the difference below.

A module is just a namespace that you can add statements to:

print "this is a module"

is a perfectly valid file module to run in python. A module may define many variables, functions, and classes. The following is a valid module.

my_variable1 = 5
my_variable2 = "hello"

def my_function1():
    return 3
def my_function2():
    return "world"

class A:
     pass

class B:
     pass

Classes are just objects that can be instantiated by calling them like below:

my_object_a = A() #instantiate class A and store the new object in variable my_object_a
Andrew Johnson
  • 3,078
  • 1
  • 18
  • 24
0

(1) That is a just a module (which is usually just a file), not a class. A class looks like this

class Foo:
    def __init__(self):
        self.initialized = True

(2) __name__ is a special variable. It comes from the environment. If you used import on this file, it would be the filename (without .py) instead of '__main__'. There are lots of "magic" variables & methods in python, and they are (almost?) always denoted by the __<name>__ convention.

(3) The point of passing self is the same as using this in Java, but in python, it's explicitly passed into the function instead of just being magically there. No, you don't need to pass it to the main, because it's not within a class.

(4) The if statement takes care of determining that this is the file that has been invoked from the command line, and hasn't simply been imported. This link shows you a well designed pattern for this.

Python takes a fairly different approach to programming than Java. Expand your knowledge! Read articles and books suggested in the comments.

forivall
  • 9,504
  • 2
  • 33
  • 58