0

how to call a function as an argument inside of the function (python)

This is very hard to explain. I might as well just show what I'm trying to do. class Player: def __init__(self, x, y, w, h): self.x = x self.y = y self.w = w self.h = h def keyMove(action): action() As you can see, I will pass a function as a parameter and call it inside the function. Let's just pretend the key functions I will use exist hypothetically. I planning to use it like this: for event in pygame.event.get(): if event.type == UP_ARROW: p.keyMove(upFunction()) elif event.type == DOWN_ARROW: p.keyMove(downFunction()) To clarify, when the player clicks the up arrow, a function gets called, same for down arrow How can I call the functions parameters inside the original function? Sorry, this was very hard to explain.

15th Jul 2020, 10:12 AM
Clueless Coder
Clueless Coder - avatar
8 Answers
0
Last time that happened, I had forgotten the self parameter
15th Jul 2020, 10:43 AM
Nor'wester 🌪️ 🇧🇩 (INACTIVE)
Nor'wester 🌪️ 🇧🇩 (INACTIVE) - avatar
+ 1
The function is returning a value, right? https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2459/ I dunno
15th Jul 2020, 10:22 AM
Nor'wester 🌪️ 🇧🇩 (INACTIVE)
Nor'wester 🌪️ 🇧🇩 (INACTIVE) - avatar
15th Jul 2020, 11:56 AM
Nor'wester 🌪️ 🇧🇩 (INACTIVE)
Nor'wester 🌪️ 🇧🇩 (INACTIVE) - avatar
0
🇧🇩🇳‌🇴‌🇷‌'🇼‌🇪‌🇸‌🇹‌🇪‌🇷‌ I don't know. It comes throwing a t type error. To simplify it to something else def thing(func): return func() def hi(): print("hi") thing(hi()) that should output "hi"
15th Jul 2020, 10:34 AM
Clueless Coder
Clueless Coder - avatar
0
Bagon It says: "Player object not callable"
15th Jul 2020, 10:37 AM
Clueless Coder
Clueless Coder - avatar
0
Bagon I tried something else: def moveKey(action): return action() p = Player() def hi(): print("test") p.moveKey(hi) It gives an error: moveKey() takes 1 positional argument but 2 were given. How? I literally gave it 1 argument!
15th Jul 2020, 10:42 AM
Clueless Coder
Clueless Coder - avatar
0
🇧🇩🇳‌🇴‌🇷‌'🇼‌🇪‌🇸‌🇹‌🇪‌🇷‌ Works! I don't know how that works! I didn't even mentioned the __init__ parameters so I wonder why it throws an error
15th Jul 2020, 10:45 AM
Clueless Coder
Clueless Coder - avatar