Functions are objects in Python
I was playing around with objects in Python to seek out the differences between Java object. I found out that functions are objects in Python. It's useful to know; you can do all sort of despicable things that other programing languages would freak out. Let's me show you why with some examples.
__________________________________________________________________________
def happy(word = “I’m happy”):
return word.capitalize() + “!”
>>>print(happy())
# output: I’m happy!
x = happy
>>>print(x)
# output: <function happy at 0x0311D9C0>
>>>print(x())
#output: I’m happy!
______________________________________________________________________________
Since happy() is a function, it is also an object. So that means you can assign the function to a variable like any other object. Notice that it's not using parentheses, cause we are not calling the function, we are assigning the function happy to the variable x. It means you can then call the function happy from x. Moreover, you can delete the name happy and the function will still be accessible from x
____________________________________________________________________________
>>>del happy
>>>print(happy())
# output: Traceback (most recent call last File "<pyshell#9>", line 1, in <module> happy() NameError: name # 'happy' is not defined
>>>print(x())
#output: I’m happy!
______________________________________________________________________________
You may ask, what's the big deal that a function can be assigned to a variable. How about if I tell you that since a function is an object; a function can be defined inside another function, which means a function can return another function! Is pure evil doing that, but no one can stop you. Let me show you how to do it, but don't tell your professor that you learn it from here.
_______________________________________________________________________________
def punch(type="cry"):
def cry(word="whooa!"):
return word.capitalize() + "!"
def punch_back(word="let's fight"):
return word.capitalize() + "!!!!"
if type == "cry":
return cry
else:
return punch_back
>>>print( punch())
#output: Whooa!
>>>print(punch("punch_back")()):
#output: Let's fight!!!!
_________________________________________________________________________
You can even pass a function as a parameter!
_________________________________________________________________________
def greetings(word="hey"):
return word
def foo(func):
return func + " you!"
>>> print(greetings())
#outputs: hey
>>>print(foo(greetings()))
#outputs: hey you!
____________________________________________________________________________
Those are some examples of the power of Python variable that one can abuse.
def happy(word = “I’m happy”):
return word.capitalize() + “!”
>>>print(happy())
# output: I’m happy!
x = happy
>>>print(x)
# output: <function happy at 0x0311D9C0>
>>>print(x())
#output: I’m happy!
______________________________________________________________________________
Since happy() is a function, it is also an object. So that means you can assign the function to a variable like any other object. Notice that it's not using parentheses, cause we are not calling the function, we are assigning the function happy to the variable x. It means you can then call the function happy from x. Moreover, you can delete the name happy and the function will still be accessible from x
____________________________________________________________________________
>>>del happy
>>>print(happy())
# output: Traceback (most recent call last File "<pyshell#9>", line 1, in <module> happy() NameError: name # 'happy' is not defined
>>>print(x())
#output: I’m happy!
______________________________________________________________________________
You may ask, what's the big deal that a function can be assigned to a variable. How about if I tell you that since a function is an object; a function can be defined inside another function, which means a function can return another function! Is pure evil doing that, but no one can stop you. Let me show you how to do it, but don't tell your professor that you learn it from here.
_______________________________________________________________________________
def punch(type="cry"):
def cry(word="whooa!"):
return word.capitalize() + "!"
def punch_back(word="let's fight"):
return word.capitalize() + "!!!!"
if type == "cry":
return cry
else:
return punch_back
>>>print( punch())
#output: Whooa!
>>>print(punch("punch_back")()):
#output: Let's fight!!!!
_________________________________________________________________________
You can even pass a function as a parameter!
_________________________________________________________________________
def greetings(word="hey"):
return word
def foo(func):
return func + " you!"
>>> print(greetings())
#outputs: hey
>>>print(foo(greetings()))
#outputs: hey you!
____________________________________________________________________________
Those are some examples of the power of Python variable that one can abuse.
This was an interesting observation Carlos, and good job with the examples.
ReplyDelete