2

I have written a script that is pretty temperamental with indentation, so I decided to make functions. I'm pretty new to Python and now that I've created these functions, nothing works!

def main():
    wiki_scrape()
    all_csv()
    wiki_set = scraped_set('locations.csv')
    country_set = all_set('all.csv')
    print wiki_set

I'm just wondering if this is the correct way to call functions from the main() function? I've been debating if an indentation issue is occurring within the called functions. Python seems to be very reliant on proper indentations even though it doesn't come up with an error!

Full Code - http://pastebin.com/gJGdHLgr

11
  • If all your functions look like main() then you have no problems. Commented Apr 15, 2015 at 13:59
  • 6
    Do you call main? Python isn't Java where the main object is called on running. Commented Apr 15, 2015 at 14:00
  • Make sure you call main. You call it like this: main(). Commented Apr 15, 2015 at 14:01
  • so I would have to call main() at the start of main() ? Commented Apr 15, 2015 at 14:02
  • 7
    "nothing works" is not a precise definition of an issue Commented Apr 15, 2015 at 14:03

2 Answers 2

10

It sounds like you need to do this:

def main():
    wiki_scrape()
    all_csv()
    wiki_set = scraped_set('locations.csv')
    country_set = all_set('all.csv')
    print wiki_set

main() # This calls your main function

Even better:

def main():
    wiki_scrape()
    all_csv()
    wiki_set = scraped_set('locations.csv')
    country_set = all_set('all.csv')
    print wiki_set

if __name__ == '__main__':
    main() # This calls your main function

Then run it from the command line like this:

python file_name.py

The built-in variable __name__ is the current contextual namespace. If you run a script from the command line, it will be equivalent to '__main__'. If you run/import the .py file as a module from somewhere else, including from inside the interpreter, the namespace (inside of the context of the module) will be the .py file name, or the package name if it is part of a package. For example:

## File my_file.py ##
print('__name__ is {0}'.format(__name__))
if __name__ = '__main__':
    print("Hello, World!")

If you do this from command line:

python my_file.py

You will get:

__name__ is __main__
Hello, World!

If you import it from the interpreter, however, you can see that __name__ is not __main__:

>>> from my_file import *
>>> __name__ is my_file
Sign up to request clarification or add additional context in comments.

10 Comments

That's pretty weird, why does python allow for code outwith the functions? I've noticed this wehn trying things but I've never understood it.
This allow you to run python files as scripts (like python myscript.py from the commandline). It also allows there to be setup code that is run when the file is imported as a module.
main doesn't mean anything in Python like it does in other languages. You can call your main program anything you like. You just have to call it.
@Gaddi, that's a big question. RickTeachey definitely started on it, procedural is very much based on a linear algorithm (do this then that). If you want to get more into it you'll need to read around. there's a decent comparison between functional and OOP on SO here: stackoverflow.com/questions/2078978/…
"why does python allow for code outwith the functions?" : because even the def and class statements are executable statements, and are indeed executed when the module is imported or executed. In fact except for byte-code compilation everything in Python happens at runtime.
|
4

Python doesn't call any functions on starting unless explicitly asked to (including main).

Instead Python names the files being run, with the main file being run called __main__.

If you want to simply call the main function you can use Rick's answer.

However in Python best practice it is better to do the following:

if __name__ == '__main__':
    wiki_scrape()
    all_csv()
    wiki_set = scraped_set('locations.csv')
    country_set = all_set('all.csv')
    print wiki_set

This ensures that if you are running this Python file as the main file (the file you click on, or run from the command line then these functions will be run.

If this is instead used as an imported module, you can still use the functions in the script importing the module, but they will not be called automatically and thus won't interfere with the calling script.

6 Comments

Using this you put wiki_set and country_set in the global namespace, so I'd prefer the usage of an extra main function.
I would question whether that's best practice or stylistic preference. I've seen well-written code that does it both ways, depending on whether the intention of the author is that a user may want to call the main() function from another script or not.
@Doug, I've certainly done it using main as a function to be called. But normally only when I have a module dedicated to a function which will only be used elsewhere. I have rarely seen someone using a main function in a script which is standalone - but that might just be the people I work with.
@Scironic - You may well be right. I know that when I learned to program Python (and before that even...gasp...Perl), I was always taught that it's desirable to put that code in a standalone main() function so that it could be imported as a module and used in the way that I intended even if I can't think of a use case for it. This is so ingrained in me that I even do it if I'm hammering out VBS or Javascript.
@Doug Either way I reckon you are probably right that it is more stylistic than best practice.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.