Kishor
Kishor

Reputation: 450

Unable to understand a syntax in python

I am new to python.I was analyzing some code snippets to realize how far I've come,to check if I had missed some key concepts of python and I got stuck here(a part of a keylogger program).

def OnKeyboardEvent(event):
    #statements...
    return True

hookMgr = pyHook.HookManager()       
hookMgr.KeyDown = OnKeyboardEvent     # stuck here!

OnKeyboardEvent is an identifier for a function right?And to call functions we should have done like:

hookMgr.KeyDown = OnKeyboardEvent(some_argument) 

My question is Why no parenthesis and arguments were used?Is this some different concept that I dont know?

I even tried to analyze what had happened doing this:

def foo(var):
  return True

ires = foo
if ires == True:
  print 'done'

It printed nothing.So does it mean identifier 'foo' as a function and 'foo' as a variable are different?

Upvotes: 3

Views: 102

Answers (3)

Ihor Pomaranskyy
Ihor Pomaranskyy

Reputation: 5611

I'd show you some example which I believe will help you to understand this Python's feature.

def calc(func, a, b):
    return func(a, b)

def add(a, b):
    return a + b

def sub(a, b):
    return a - b

print(calc(add, 2, 2))  # 4
print(calc(sub, 42, 1))  # 41

That's it. You can pass some function as argument of the other function, or store it in some variable and then call it etc. It's really useful, but a bit unusual.

By the way, my favorite usage of this feature: using dictionary with functions instead of (missing) switch .. case operator. Like this:

def get_serialized_value(obj):
    serializer = {
        Article: article_brief_serialize,
        RichTextArticle: rich_text_article_brief_serialize,
        Gallery: gallery_serialize
    }.get(type(obj))
    return serializer(obj).data if serializer else None

# what was replaced by the code above..
def get_serialized_value(obj):
    if type(obj) == Article:
        return article_brief_serialize(obj)
    elif type(obj) == RichTextArticle:
        return rich_text_article_brief_serialize(obj)
    elif type(obj) == Gallery:
        return gallery_serialize(obj)
    else:
        return None

Upvotes: 1

Phoenix
Phoenix

Reputation: 1788

A great thing about python is that functions and classes are themselves objects, and can thus be passed around as variables. For example, a function that has another function as an argument could be something like

def foo(bar):
    bar()

In which you can call it and input the name of any function, and it will call the function you input with no arguments. Often this is used in event handler models, where you will pass an object a reference to a function, and when an event happens, the object will call your function, which is what is happening in your called.

Upvotes: 0

TigerhawkT3
TigerhawkT3

Reputation: 49320

Yes, OnKeyboardEvent is a reference to the function itself, while OnKeyboardEvent() is a reference to the result of calling the function. hookMgr.KeyDown = OnKeyboardEvent connects hookMgr.KeyDown to the given function, similarly to the way tkinter.Button(command=myfunction) would connect a function, not the value returned by a function call, to a Tkinter button.

Upvotes: 3

Related Questions