I want to learn about def because that looks very the most intriguing how you can make it kind of like reusable
but when I was learning I had come some problems with understanding

def main():
    #renewable code

main()

if __name__ == "__main__":
    main()

I don't understand what __name__ and __main__ is but I can remember learning something about magic and then nothing makes any sense to me anymore after that

I think there is something like main() is the default but can be called anything but the first one must be main() or something like that

Edited 3 Years Ago by nouth

A Python file can either be run as a program or it can be imported by another Python file as a library. if __name__ == "__main__": means "only run the following code, if this file was run as a program - not imported as a library".

This is useful if you want other program's to be able to import this program's functions without actually running the program.

can it be like this

def anything():
    #renewable code

anything()

if __name__ == "__anything__":
    anything()

or does it have to be main

You can name your function anything you want, but the name has to be "__main__". That's not about the function being called. "__main__" is simply the value that __name__ gets if the Python file is invoked directly instead of being imported.

PS: Calling your function both inside and outside of the if means that your function will be called once if the file is imported and twice if the file is run directly. That doesn't seem like what you'd want.

so the if statement isn't required?
what is the __name__ by default?

I don't understand what you mean when you say "if the Python file is invoked directly", does that mean double clicking on a .py file for it to run or can I run a .py file using python?

I think import can be used to access functions from another .py file but I'm not sure and haven't used it yet

Edited 3 Years Ago by nouth

so the if statement isn't required?

You only need the if statement if you want your code to act differently depending on whether it's imported or run as a program.

what is the __name__ by default?

If the Python file is run as a program (i.e. using python bla.py, a double click in the file manager (assuming you have set it up, so that that actually runs the program rather than opening it in an editor), using ./my_file.py in combination with chmod +x and a shebang line -- whichever), __name__ will be "__main__". If it is imported from another Python file or from the Python shell by writing import my_module, __name__ will be "my_module" where my_module is the name of your file without the .py extension.

I don't understand what you mean when you say "if the Python file is invoked directly" does that mean double clicking on a .py file

See above.

or can I run a .py file using python?

That too.

Basically running it directly refers to any means of running your Python code other than using it from other Python code.

I think import can be used to access functions from another .py file

Yes.

I think import can be used to access functions from another .py file but I'm not sure and haven't used it yet

Yes and here dos if __name__ == "__main__": play and important part.
You have made a fantastic name multiplier code,and you whant to import it so you can use it in other code or share it.
Version 1.

#name_multiplier.py
def name_multiplier(*arg):
    return arg * 2

#Tests
print name_multiplier('hello', 'world')
print name_multiplier(1, 9)

Save it as name_multipler.py in PYTHONPATH,just use python27 folder or your version of Python folder.
Now code is an module and you can import it,ohh easy.
Test it out.

>>> import name_multiplier
('hello', 'world', 'hello', 'world')
(1, 9, 1, 9)

Hmm i dont want test code to get run when i import it.
Version 2.

#name_multiplier.py
def name_multiplier(*arg):
    return arg * 2

if __name__ == '__main__':
    #Tests
    print name_multiplier('hello', 'world')
    print name_multiplier(1, 9)

Test it out.

>>> import name_multiplier
>>> #Now it works as it should and dont run test code
>>> name_multiplier.name_multiplier('a', 5)
('a', 5, 'a', 5)

>>> #I want to type name_multiplier 1 time
>>> from name_multiplier import name_multiplier
>>> name_multiplier('.', '_','~')
('.', '_', '~', '.', '_', '~')

So in version 2 it work as a standalone program and also as a module should(not run code when import is used)

can I import a file from the very same folder as the file I want to run?

can I import a file from the very same folder as the file I want to run?

Yes, this is one way Python can find the import file.

but what if there is like import sys and I have a file in the same folder named sys.py because then there are will become two sys or will it import both of them?

Edited 3 Years Ago by nouth

It will not import both. When you import a module it searches through the available modules and imports the first one it finds with the given name. The order in which it goes through the modules is explained in this section of the docs:

When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:

  • the directory containing the input script (or the current directory).
  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
  • the installation-dependent default.

After initialization, Python programs can modify sys.path. The directory containing the script being run is placed at the beginning of the search path, ahead of the standard library path. This means that scripts in that directory will be loaded instead of modules of the same name in the library directory. This is an error unless the replacement is intended. See section Standard Modules for more information.

So since sys is a built-in module, it will always be found before your sys.py.

A common mistake beginners make is to name their code/script file the same as a Python module they need to import. For instace random.py will screw up the random module.

This question has already been answered. Start a new discussion instead.