Python Forum
Python calling function unexpectedly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python calling function unexpectedly
#1
Sorry for the long python code!
I'm trying to optimise this code, so I thought I'd make the if lists into a callable function. However, whenever I try to call it (last line), it runs basic_option() instead. It's nothing to do with the code in if_tree() itself, because if I comment it all out the same thing still happens. However, if I replace the list of functions with something else, it works.
Basically the problem is python is calling the function in the call for if_tree unexpectedly.

Any advice on the code in general would be great as well!

If anyone can help that would be great!
Thanks in advance.
Reply
#2
When setting a variable or sequence index to a function, do not use parentheses. Parentheses signify that the function should be called. If you use the function name by itself, the variable or index is instantiated to the function itself.

if_tree( MAIN_TEXT, ["1", "2"], ["Q", "H", "R"], [basic_option, advanced_option, quit_program, display_help, exec("restart_calculator()")] )
Other than that, I believe this script should be refactored into a class called Calculator. The function input() returns a string so there is no need for str(input()).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  C++ program embedding Python crashes when calling PyUnicode_AsUTF8 Alexandros 4 2,385 Sep-19-2025, 11:11 AM
Last Post: Alexandros
  python script is hanging while calling a procedure in database prasanthi417 4 3,123 Jan-17-2024, 02:33 PM
Last Post: deanhystad
  calling external function with arguments Wimpy_Wellington 6 3,885 Jul-05-2023, 06:33 PM
Last Post: deanhystad
  Calling a function (which accesses a library) from another file mouse9095 4 6,268 Jun-07-2023, 08:55 PM
Last Post: deanhystad
  PyRun_SimpleFile calling multiprocessing Python Class cause endless init loop Xeno 2 2,799 Sep-19-2022, 02:32 AM
Last Post: Xeno
Sad Iterate randint() multiple times when calling a function Jake123 2 4,142 Feb-15-2022, 10:56 PM
Last Post: deanhystad
  Calling a class from a function jc4d 5 3,932 Dec-17-2021, 09:04 PM
Last Post: ndc85430
  [Solved] TypeError when calling function Laplace12 2 4,537 Jun-16-2021, 02:46 PM
Last Post: Laplace12
  Calling python from c++ in visual studio pdk5 0 3,073 May-24-2021, 10:18 AM
Last Post: pdk5
  Calling Oracle REST SQL from Python johnjacob 2 3,351 Nov-05-2020, 04:19 AM
Last Post: johnjacob

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.