The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Question about calling functions.
Discuss Question about calling functions. in the Python Programming forum on Dev Shed. Question about calling functions. Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 11th, 2013, 03:29 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 3
Time spent in forums: 1 h 28 m 55 sec
Reputation Power: 0
|
|
|
Question about calling functions.
Hello. I used to script several years ago, and I would typically use 'goto' to jump to a different function. The script would basically have a large set of functions, with a starting function that would then call a different function based on what was done or typed, and then continue jumping from there.
I am having a bit of trouble now that I am trying out Python (and rusty on top of that) with being able to hop like that. Basically, I am currently trying to make something like this:
def start_game():
Welcome! Please type a command. For a list of commands, please type 'command'.
if input=command
-------goto list_commands
elif input=start
-------goto game_init
start_game()
def list_commands()
Here is the list of command:
Start
Blah
Blah
Blah
goto start_game
list_commands()
def game_init():
game_init()
Obviously this is not the syntax used, but it was just to quickly demonstrate what I mean. It asks for input, you give input, and then it calls a function based on that. In the "list_commands" function, you would get the list, and then it would return you back to the "start_game" function, so that you could then enter the commands.
I am curious how to get this to work, as various places that I have read about calling functions dont seem to make sense, or they dont work in the way I expect them to. Thanks!
|

February 11th, 2013, 04:19 AM
|
 |
Contributing User
|
|
Join Date: Nov 2012
Location: Iran / France
|
|
It seems to you want to call functions based on their names or any corresponding input parameter. May be something like the following example (just for an idea)
Code:
import sys
def f1():
print("f1 was called")
def f2():
print("f2 was called")
def f3():
print("f3 was called")
def main():
command = raw_input("Please enter the command to run: ")
commandDictionary = {"f1":f1, "f2":f2, "f3":f3}
if command in commandDictionary:
commandDictionary[command]()
else:
print("invalide command")
sys.exit(-1)
main()
Regards,
Dariyoosh
|

February 11th, 2013, 04:57 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 3
Time spent in forums: 1 h 28 m 55 sec
Reputation Power: 0
|
|
|
Ok, so there is no need to tag it with a goto, or anything like that? You just list the function with () after it, and it automatically goes to that function?
As in, instead of:
goto function1 ()
It is just a line with:
function1()
?
|

February 11th, 2013, 05:11 AM
|
 |
Contributing User
|
|
Join Date: Nov 2012
Location: Iran / France
|
|
Quote: | Originally Posted by Latsabb Ok, so there is no need to tag it with a goto, or anything like that? You just list the function with () after it, and it automatically goes to that function? | If function1 refers to a valid function name in the current module then function1() calls that function. This is precisely what I did in my example, in python you can assign functions to variables (which are in fact objects). So the values in my dictionary are in fact function type objects
Regards,
Dariyoosh
|

February 11th, 2013, 05:17 AM
|
 |
Contributing User
|
|
Join Date: Nov 2012
Location: Iran / France
|
|
|
In addition as far as I know there is no goto operator in python and I don't really know (even in other programming languages such as C, Java, ...) why someone may want to use that, IMHO it makes the code less readable.
|

February 11th, 2013, 06:07 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 3
Time spent in forums: 1 h 28 m 55 sec
Reputation Power: 0
|
|
|
Alright, I got it. Thank you very much. There was always a command that called a function when I scripted, so I was used to having that. Also, all functions needed end tags, and it seems that this isnt the case in Python, and actually messed things up a bit for me. But now I am on my way. Thank you again!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|