-1

I have used this code and it runs fine. However, there is something weird about it, it's like it's not Python!

The e variable in print_event is used in a way I haven't seen before. It's a regular function that prints whatever is passed to it, but the problem is how it's used, even the event variable that's supposed to be passed as an argument to the parameter e

If you don't pay attention, it seems like the append function returns added values to print_event, instead of appending them, like what the append does in Python.The whole function gets appended to the list of handlers once and then it keeps running until the program terminates,like it's a while True loop.

The code basically starts a keyboard listener and keeps recording key pressed keys, but what happens to the keys is the quesion. The for loop in low level listener doesn't make sense, why iterate through the handlers if it's supposed to record the keys, not read them. Besides, why pass the event? Handlers is a list, not a function, i'm only aware of the assignement operator for initializing variables

Also, if handlers is initialize empty, how does it assign values to items and through them if their memory space isn't allocated and doesn't exist?

I don't seeing any buffer function being called, so how's it working? Python shouldn't look like that

What i'm trying to do is to access the handlers list in real time and process the events

An explanation would be appreciated. Thanks in advance

Waleed Qutob
  • 57
  • 1
  • 3
  • 7
  • Please add the code you are questioning about *in the question itself*. Links are not appropriate, and can change or break for future users reading your question. – Mark Tolonen Feb 15 '20 at 00:59
  • @Mark Tolonen it's about the indention, I didn't want to confuse people who try to answer – Waleed Qutob Feb 18 '20 at 22:37

1 Answers1

0

Are you asking about Function Variables?

If yes, you can pass around functions like any other variable, and call them later by a different name.

EG:

def hi(string):
    print(string)

fns = [hi, hi]

for fn in fns:
    fn('hello')

If that remains puzzling, perhaps you could step through it with a debugger to make the idea seem more concrete.

dstromberg
  • 6,954
  • 1
  • 26
  • 27
  • Thanks for the answer. I knew that it's possible to iterate to functions like objects but there is no point. The function could be called directly. What I was asking about is the print function that's being used in the append in the "main loop". I'd appreciate an explanation, it doesn't look possible to me. I want to make an instance of that class and access the handlers values directly, but thr handlers variable stays empty, is a buffer used? Thanks in advance. By the way, a link to the code is available if you click on the word code in the beginning – Waleed Qutob Feb 18 '20 at 23:48