Python store function in variable with arguments
By far easiest is just to use lambda in place Show
Though, in a similar but a bit more complicated cases, you really should use a function factory such as
because it would make sure that you pass in correct arguments (not Also, hardcoding the questions / answers in the button code does not seem right... View Discussion Improve Article Save Article View Discussion Improve Article Save Article In this article, we are going to see how to assign a function to a variable in Python. In Python, we can assign a function to a variable. And using that variable we can call the function as many as times we want. Thereby, increasing code reusability. Implementation Simply assign a function to the desired variable but without () i.e. just with the name of the function. If the variable is assigned with function along with the brackets (), None will be returned. Syntax: def func(): { .. } var=func var() var() Example: Python3
Output: GFG The following programs will help you understand better: Example 1: Python3
Output: 123 98 123 98 123 Example 2: parameterized function Python3
Output: odd number even number odd number Example 3: Python3
Output: 240 400 4000 Recommended Answers
Jump to Post
Jump to Post All 8 Replies
redyugi 5 Junior Poster in Training Not unless you are going to use "exec()" and people advise against that. The second way is a good way.
bumsfeld 413 Nearly a Posting Virtuoso 12 Years AgoSimply store the reference to the function in the variable. Here is example:
redyugi 5 Junior Poster in Training 12 Years Ago
I never knew I could do that. I knew you could do it with a class instance but...thanks
12 Years Ago Awesome. That solves a tonne of problems. Thanks. =)
4 Years Ago I need help on my Python project. I am creating a guess the number game and I need to include a menu, collect statistics, and plot statistics results. I have already created the game, but I don't know how to include a menu, collect statistics, or plot the results. Please help!!
3 Years Ago Hi, going wrong if the function is specified with "self" Ex:
Please help me with this...
3 Years Ago Hi @Akshay_15, Why not post your question as a new topic instead of commenting on an old post here ? Edited 3 Years Ago by John_165
1 Year Ago
This would be more general solution. Edited 1 Year Ago by tomas_petricek Reply to this topic Be a part of the DaniWeb community We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge. Can you store a function in a variable Python?In Python, we can assign a function to a variable. And using that variable we can call the function as many as times we want.
How do you store functions in a variable?Functions stored in variables do not need function names. They are always invoked (called) using the variable name. The function above ends with a semicolon because it is a part of an executable statement.
Can I put a function inside a list Python?Answer: You can use any expression inside the list comprehension, including functions and methods. An expression can be an integer 42 , a numerical computation 2+2 (=4) , or even a function call np. sum(x) on any iterable x . Any function without return value, returns None per default.
How do you call a function variable in Python?To use functions in Python, you write the function name (or the variable that points to the function object) followed by parentheses (to call the function). If that function accepts arguments (as most functions do), then you'll pass the arguments inside the parentheses as you call the function.
|