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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gaddi
  • 167
  • 1
  • 3
  • 16

2 Answers2

8

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rick
  • 43,029
  • 15
  • 76
  • 119
  • 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. – Gaddi Apr 15 '15 at 14:04
  • 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. – TheBlackCat Apr 15 '15 at 14:06
  • `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. – Rick Apr 15 '15 at 14:07
  • @gaddi, do you mean without functions? It's simply because Python, provides you with options. You can use Python as a functional language, an OO language or as a simple procedural language. Or a mix of everything. You get to decide what is best for the project you are working on. – NDevox Apr 15 '15 at 14:16
  • @Scironic So when using functions it becomes more object orientated and less scriptive when without? – Gaddi Apr 15 '15 at 14:19
  • @Gaddi By definition it becomes more based in functional programming, although considering that python functions are objects, it could also be seen as OO. But yes it will be less procedural. – NDevox Apr 15 '15 at 14:21
  • @Scironic Thanks for the understanding! Still very new to programming in general with this being my first big project. So how would you be able to tell if a project required OO or procedural? – Gaddi Apr 15 '15 at 14:24
  • @Gaddi That's a hard question to answer. Generally speaking, the more complex a project, the easier it will be to implement using an OO approach. If you find that your project is "do this, then this, then this", you'll probably be fine sticking to procedural style. If you start dealing with "things", it's time to consider OOP. – Rick Apr 15 '15 at 14:27
  • 1
    @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: http://stackoverflow.com/questions/2078978/functional-programming-vs-object-oriented-programming – NDevox Apr 15 '15 at 14:30
  • 1
    "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. – bruno desthuilliers Apr 15 '15 at 15:30
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
NDevox
  • 4,056
  • 4
  • 21
  • 36
  • 3
    Using this you put `wiki_set` and `country_set` in the global namespace, so I'd prefer the usage of an extra main function. – Matthias Apr 15 '15 at 14:15
  • 1
    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. – Deacon Apr 15 '15 at 14:16
  • @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. – NDevox Apr 15 '15 at 14:18
  • @Matthias, that's a great point. Although, assuming the variables will be used later on, I would have put their definitions in a function and called it using the above method. – NDevox Apr 15 '15 at 14:20
  • 2
    @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. – Deacon Apr 15 '15 at 14:29
  • 1
    @Doug Either way I reckon you are probably right that it is more stylistic than best practice. – NDevox Apr 15 '15 at 14:31