Can someone explain the implementation of function as objects? It's quite confusing and I'm having a hard time understanding it. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Can someone explain the implementation of function as objects? It's quite confusing and I'm having a hard time understanding it.

Function as Objects

2nd Mar 2017, 6:35 AM
Nikhil Thakur
Nikhil Thakur - avatar
9 Answers
+ 9
def thing(): print("thing") thing() #def thing() is the function, and won't be executed until you put thing() as a object in your code
2nd Mar 2017, 6:44 AM
Ahri Fox
Ahri Fox - avatar
+ 5
Like in mathematics, you use function quiet same way... giving data, you expect a corresponding result: f(x)=ax+b give the value of the corresponding y value for (x,y) dot on the line described by the expression. Basically, it's what you do to in programming. With Python, equivalent function is defined by: def get_y(x): var a = 4 var b = 2 return a*x+b ... and it's used: var point_y = get_y(10); In programming, contrarely to matematics, you need often some block of code to be reuse many times, but not necessarly wich return a value... Sometimes we made a distinction between function who's provide return value or not by distinct name: the second one are called "procedure"... and also functions or procedures belonging to a class ( OOP ) are called indifferently "method". But usually they all are as well called "function" :P
2nd Mar 2017, 12:01 PM
visph
visph - avatar
+ 3
>>> help("def") # just the interesting bits Function definitions ******************** A function definition defines a...function object...[and] is an executable statement [. it] binds the function name...to a function object (a wrapper around the executable code for the function)...**Programmer's note:** Functions are first-class objects... ----- Takeaway: Write "def demo():" and Python cleverly wraps demo's body in a class (function type): >>> def demo(): print("You called?"); >>> print(type(demo)) <class 'function'> It has an attributes directory. >>> print(dir(demo)) ['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__'] Look at all the things we can do with our function object. >>> demo.__call__() You called? Look deeper? demo.__code__'s also a class (code type). >>> cheat = demo.__code__ >>> type(cheat) <class 'code'> >>> dir(cheat) [ ... more attributes like above ... 'co_code', 'co_consts', 'co_filename' ... ] >>> print([cheat.co_filename, cheat.co_argcount, cheat.co_consts, cheat.co_names, cheat.co_nlocals, cheat.co_varnames]) ['<stdin>', 0, (None, 'You called?'), ('print',), 0, ()] 'stdin' - yep, I'm in interactive>>> mode. Zero arguments, string constants tuple, names tuple (print function), no local variables, empty variable name tuple. As a quick tour of Python objects that happen to relate to functions...it's not code...but I can if it's helpful.
3rd Mar 2017, 2:34 AM
Kirk Schafer
Kirk Schafer - avatar
+ 3
Also...you might like a GUI to inspect Python objects: https://github.com/titusjan/objbrowser Request 'desktop site' on mobile, then scroll down to "User interface" for a sample. Install with pip.
3rd Mar 2017, 3:34 AM
Kirk Schafer
Kirk Schafer - avatar
+ 3
Since you're passing the "add" object, you can mentally replace "func" with "add" (also: x=a and y=b...I didn't do that): def do_twice(add, x, y): return add(add(x,y), add(x,y)) Evaluate from innermost to outermost: return add(x+y, x+y) becomes: return (x+y) + (x+y) # effectively 2*(a+b) The reason it's done this way (I think) is so you could substitute other functions without much trouble (subtract, concatenate, something weirder..). I would expect this self-similarity in a fractal.
3rd Mar 2017, 4:03 AM
Kirk Schafer
Kirk Schafer - avatar
+ 2
When python interpreter see reserved name-def, he doesn't run the code under def, just adding function name to namespace
2nd Mar 2017, 7:14 AM
Leshark
Leshark - avatar
+ 1
I'm actually looking for an analogy that can help me understand with an example of a wrong & working code or may be a detailed explanation. I really appreciate your quick response and please pardon my ignorance.
2nd Mar 2017, 7:17 AM
Nikhil Thakur
Nikhil Thakur - avatar
0
Thank you all for the help👍! Can someone explain this code to me bit-by-bit? def add(x, y): return x + y def do_twice(func, x, y): return func(func(x, y), func(x, y)) a = 5 b = 10 print(do_twice(add, a, b)) Basically, line 3 & 4 of the code. Thank you in advance!
3rd Mar 2017, 3:49 AM
Nikhil Thakur
Nikhil Thakur - avatar
0
@Kirk: Spot on! Thanks a lot for the explanation 👍
5th Mar 2017, 12:20 PM
Nikhil Thakur
Nikhil Thakur - avatar