Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function as parameter
#1
hello and i have a question about python

are you able to add a function as a parameter and still run it?

here's what it would look like:
def function_load(function):
    function() # parameter

def add(x, y):
    return x + y

print(function_load(add(x, y)))

# or...

# this in tkinter
#                 func
window.after(0, forever)
Reply
#2
Something like

def function_load(function, *params):
    return function(*params) # parameter
 
def add(x, y):
    return x + y

x = 3
y = 5
print(function_load(add, x, y))
there might be better approach, depending what exactly you want to achieve
jefsummers likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
With First Class Functions In Python, you can:
  • assign functions to names
  • pass them along as arguments
  • store them in bigger data structures
  • define them inside another function and also return them from another function just like any other objects
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  determine parameter type in definition function akbarza 1 1,201 Aug-24-2023, 01:46 PM
Last Post: deanhystad
  Function parameter not writing to variable Karp 5 2,069 Aug-07-2023, 05:58 PM
Last Post: Karp
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 17,311 Dec-26-2022, 08:48 AM
Last Post: ibreeden
  function with 'self' input parameter errors out with and without 'self' called dford 12 7,207 Jan-15-2022, 06:07 PM
Last Post: deanhystad
  use NULL as function parameter which is a pointer function? oyster 0 3,019 Jul-11-2020, 09:02 AM
Last Post: oyster
  what would you call the input for the parameter(s) of a function you have defined? rix 3 3,240 Dec-16-2019, 12:04 AM
Last Post: rix
  def function default parameter expressions Skaperen 2 3,157 Aug-22-2019, 10:58 PM
Last Post: Skaperen
  Recursion, with "some_dict={}" function parameter. MvGulik 3 3,243 Aug-02-2019, 01:34 PM
Last Post: MvGulik
  Input as function parameter dan789 1 2,618 Mar-08-2019, 05:19 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020