Results 1 to 8 of 8

Thread: Python: functions has to be define first in the code to work?

  1. #1
    Join Date
    Apr 2006
    Location
    Copenhagen
    Beans
    91
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Python: functions has to be define first in the code to work?

    I probably misunderstood something but as I see it now I need to define a function before a call in Python. This makes sense when thinking of Python as a scripting language - but I am using it as programming language and this problem really makes my code look ugly and confusing.

    Did I misunderstand something or is there really no way around this?
    Kind regards
    Thomas Jansson - www.tjansson.dk
    Geek By Nature | Linux By Choice

  2. #2
    Join Date
    Apr 2007
    Beans
    14,781

    Re: Python: functions has to be define first in the code to work?

    That happens in many languages.

    To make the code clean, put the function in a different file, then import them.

    This also makes them reusable.

  3. #3
    Join Date
    Dec 2005
    Location
    Atlanta, GA, USA
    Beans
    233
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Python: functions has to be define first in the code to work?

    I think it's a matter of taste, personally. I like having my functions and classes up top. To eliminate confusion, just comment your code:

    Code:
    import os
    
    # ---- Start Functions ----#
    
    # some_func - A cool function
    def some_func():
       print "hi"
    
    # ---- End Functions ---- #
    
    some_func()
    I do agree, though, if you have code you're going to reuse in other apps, go ahead and move that out to a separate file.
    Linux User #343227 | Ubuntu User #13191 | Website | Launchpad Profile | Twitter

  4. #4
    Join Date
    Jun 2006
    Location
    CT, USA
    Beans
    5,267
    Distro
    Ubuntu 6.10 Edgy

    Re: Python: functions has to be define first in the code to work?

    Quote Originally Posted by tjansson View Post
    as I see it now I need to define a function before a call in Python. This makes sense when thinking of Python as a scripting language - but I am using it as programming language and this problem really makes my code look ugly and confusing.
    Not sure how you differentiate between "scripting" and "programming" , and how writing readable code makes your code ugly.

    But order you mentioned is necessary - you wished for fast single-pass source code parser, didn't you? So you got your wish. Be more careful next time.

    I hazard to guess you have huge main program with some functions at the end. You can easily convert it to this format:

    Code:
    def main():
        sub1()
        sub2()
    
    def sub1():
        # code
    def sub2():
        # code
    
    if __name__ == "__main__":
        main()
    See also Guido's blog post about writing main() the right way. And trust me, Guido knows what he is talking about

    This way, your module will run main() if called as main program, and not run it if imported. You can have your cake and eat it too.
    Last edited by pmasiar; August 1st, 2007 at 06:10 PM.

  5. #5
    Join Date
    Jan 2007
    Location
    Waterloo
    Beans
    24
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Python: functions has to be define first in the code to work?

    Functions gotta go first, any procedural code goes at the bottom. Importing of functions is nice to do but not always practical depending on what they do. Also, i found this really confusing when i just got into python -

    Code:
    if __name__ == "__main__":
        main()
    That's something you only need if you ever plan on importing this file as a module in another. It essentially says "if you're running this as a script then execute main" but if you're importing it to another program nothing is going to execute unless explicitly called.

    Hope that clears stuff up

  6. #6
    Join Date
    Jun 2006
    Location
    CT, USA
    Beans
    5,267
    Distro
    Ubuntu 6.10 Edgy

    Re: Python: functions has to be define first in the code to work?

    if doing it flexible and right is confusing, instead of

    Code:
    if __name__ == "__main__":
        main()
    do just at the last line
    Code:
    main()
    and make sure you do not import that module (because main() would be executed).

  7. #7
    Join Date
    Apr 2006
    Location
    Copenhagen
    Beans
    91
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Python: functions has to be define first in the code to work?

    Thanks for the advice guys. My problems is that I have a class for some buttons. The buttons have a disable method but I can't disable a given button before I have initiated it - this makes sense off course but It makes the code look bad since I have initiate all the button in top and draw later on.
    Kind regards
    Thomas Jansson - www.tjansson.dk
    Geek By Nature | Linux By Choice

  8. #8
    Join Date
    Jun 2006
    Location
    CT, USA
    Beans
    5,267
    Distro
    Ubuntu 6.10 Edgy

    Re: Python: functions has to be define first in the code to work?

    wrong. just put all code you have in top (module) level into a main() function, and call this function at last line.

    names have to be known only at at execution time - can you post your (simplified) code sample?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •