LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-11-2009, 09:50 PM   #1
snowman81
Member
 
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 277

Rep: Reputation: 30
Make entire python program a function?


If I have a program I wrote in Python can I turn it into a function of a bigger program? For instance, I have a program that does something with numbers. I found it somewhere but it doesn't matter what it does.
Code:
def median(pool):
    '''Statistical median to demonstrate doctest.
    >>> median([2, 9, 9, 7, 9, 2, 4, 5, 8])
    7
    '''
    copy = sorted(pool)
    size = len(copy)
    if size % 2 == 1:
        return copy[(size - 1) / 2]
    else:
        return (copy[size/2 - 1] + copy[size/2]) / 2
if __name__ == '__main__':
    import doctest
    doctest.testmod()
Can I define it and return whatever value this gives? Something like this?

Code:
def number_function();

    def median(pool):
    '''Statistical median to demonstrate doctest.
    >>> median([2, 9, 9, 7, 9, 2, 4, 5, 8])
    7
    '''
    copy = sorted(pool)
    size = len(copy)
    if size % 2 == 1:
        return copy[(size - 1) / 2]
    else:
        return (copy[size/2 - 1] + copy[size/2]) / 2
if __name__ == '__main__':
    import doctest
    doctest.testmod()

    return (whatever);
 
Old 01-11-2009, 10:17 PM   #2
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
Short answer: no.

If you want syntax like that, you will have to define a class around that number_function(). What exactly do you want out of this program? Putting an algorithm into a function makes it re-usable. In the second example, I believe the program will fail because you are not within a functional block - the return() statement will throw an error.
 
Old 01-12-2009, 01:14 AM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Disclaimer: I am not a Python guy.

Anyway, traditionally 'main' returns 0 on success and non-zero on failure. This alone makes the approach problematic.

The approach may work for void functions.
 
Old 01-12-2009, 02:17 AM   #4
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Code:
def _main(argv=None):
    # Write your main program code here.

if __name__ == '__main__':
    _main()
Then, in your other module, you could just call stats._main() or whatever.
 
Old 01-12-2009, 05:57 AM   #5
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Quote:
Originally Posted by Sergei Steshenko View Post
Anyway, traditionally 'main' returns 0 on success and non-zero on failure. This alone makes the approach problematic.
The Python if __name__ == '__main__' idiom is to run certain code when the file is executed as a program but not when loaded as a module. It is not totally analogous to the main() method in C and friends because it does not constitute a function and therefore cannot return a status code. You would use os.exit() for that rather than return.
 
Old 01-12-2009, 02:04 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by taylor_venable View Post
The Python if __name__ == '__main__' idiom is to run certain code when the file is executed as a program but not when loaded as a module. It is not totally analogous to the main() method in C and friends because it does not constitute a function and therefore cannot return a status code. You would use os.exit() for that rather than return.
I think you are missing my point.

The point is that either a piece of code returns value(s), or it produces some side effect, like setting global variable(s) or performing IO.

So regardless of language/idioms, if originally some piece of code was meant to return value(s) and the context doesn't allow this, then what the code was run for in the first place ?
 
Old 01-12-2009, 06:42 PM   #7
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Quote:
Originally Posted by Sergei Steshenko View Post
I think you are missing my point.

The point is that either a piece of code returns value(s), or it produces some side effect, like setting global variable(s) or performing IO.

So regardless of language/idioms, if originally some piece of code was meant to return value(s) and the context doesn't allow this, then what the code was run for in the first place ?
Sorry, this doesn't make much sense to me. "Code" in general could produce a side-effect and it could return a value. (It's not an xor relationship.) Or multiple values. Or the change in the state could be represented as the return value (the "consume the old world state, produce the new world state" model). And "multiple values" could actually be packaged transparently in an array. Or in concatenative languages be pushed onto a stack. I think idioms and languages could be very important. What if we wanted to take a function in a stack-based language that wrote a file and then returned 42 for no reason. But then called this function in a way that the return value was ignored (stack returned to the state before the function was called). That program would still have meaning, the file would still be there. Not allowing a return from a non-function is Python's way of checking you; it's an error condition which it prevents, as opposed to an alternative approach where it could simply be ignored.

snowman81: Here's what I'd do. Change this:
Code:
def foo():
    # some code for 'foo'
    return 42

if __name__ == '__main__':
    i = 7 + foo()
    print(i)
into this:
Code:
def main():
    i = 7 + foo()
    print(i)

def foo():
    # some code for 'foo'
    return 42

if __name__ == '__main__':
    main()
 
Old 01-12-2009, 07:24 PM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by taylor_venable View Post
Sorry, this doesn't make much sense to me. "Code" in general could produce a side-effect and it could return a value. (It's not an xor relationship.) Or multiple values. Or the change in the state could be represented as the return value (the "consume the old world state, produce the new world state" model). And "multiple values" could actually be packaged transparently in an array. Or in concatenative languages be pushed onto a stack. I think idioms and languages could be very important. What if we wanted to take a function in a stack-based language that wrote a file and then returned 42 for no reason. But then called this function in a way that the return value was ignored (stack returned to the state before the function was called). That program would still have meaning, the file would still be there. Not allowing a return from a non-function is Python's way of checking you; it's an error condition which it prevents, as opposed to an alternative approach where it could simply be ignored.

snowman81: Here's what I'd do. Change this:
Code:
def foo():
    # some code for 'foo'
    return 42

if __name__ == '__main__':
    i = 7 + foo()
    print(i)
into this:
Code:
def main():
    i = 7 + foo()
    print(i)

def foo():
    # some code for 'foo'
    return 42

if __name__ == '__main__':
    main()
I have already said that in one sentence:
Quote:
The approach may work for void functions.
 
Old 01-12-2009, 08:11 PM   #9
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Well admittedly this is true, but when you say "void functions" I think of C (maybe my own fault, but it is a limitation I may share with the OP) where it is a function that necessarily returns no value. This example I gave could also work where main() returns a value but when the code is executed as a standalone program that value is a discarded. In this case main() would not be a "void function" as I tend to think of it, but it would still work.

Also, I wanted to give an actual example in case that was more helpful. I don't think I've ever heard Python folks talk about "void functions" since the same Python function could return various types or nothing at all. Again, language-specific stuff, but nevertheless useful.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
need an app to make thumbnails of entire cd rip Linux - Software 2 12-11-2008 09:05 PM
can someone explain the 'for' function in python to me deathalele Programming 8 10-16-2008 12:10 PM
how will u make a program for substring in function without using string.h am_thegenius Programming 3 08-25-2008 06:34 AM
python, os.system() function. howto use python variables? jhwilliams Programming 5 07-28-2007 02:56 AM
is there a linux program that downloads an entire website? lemuel Linux - Software 10 01-06-2005 03:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration