[Tutor] Apply() [function calling depends on end parentheses]

Erik Price eprice@ptc.com
Thu Feb 6 15:20:02 2003


Danny Yoo wrote:

[...]

> The thing that makes functions unique from values, like strings and
> numbers, is that they can be "called" by using those parentheses at the
> end.  In technical terms, functions can be "applied" or "called".  In some
> programming languages, the term "apply" is prevalent, but in Python, it
> looks like "call" is the term that is most frequently used.  (Perhaps the
> 'apply()' builtin should have been renamed as 'call()'...  but I'm just
> digressing.  *grin*)

After reading the link you sent earlier in this thread, I noticed that 
the term "callable" is found all over the Python docs.  So Python is a 
lot different from Java, since I don't think that Java considers its 
"callables" (method names) to be true objects that can be passed around 
and referenced with different names.

> That's what makes something like:
> 
> ###
> 
>>>>f = sayHello
>>>>f()
> 
> hello
> ###
> 
> possible: 'f' is a name that's directed to the same function value.  The
> first statement doesn't do an application: it merely does a variable name
> assignment, just like any other assignment we've seen.  We can still get
> 'f' to fire off by doing the parentheses "application".

That cool!  I have never done something like that.  It would seem like 
there could be some obscure uses for this (like calling different 
functions from within a loop, as if the function was an object and we 
were using polymorphism) but I wonder how often they are used in 
practice?  I have no formal training in programming so I am not familiar 
with this kind of thing, but isn't this related to something called 
functional programming?  I think I have seen people on this list talk 
about Lisp having features like this (long time ago so I can't remember, 
I've been off this list for a long time).

[...]

> Please feel free to ask more questions about this.  I hope this helps!

I have one last question -- where do you learn about these kinds of 
advanced Python concepts, once you've gotten past the understanding of 
functions and basic data types, etc (other than here)?  And where do you 
see them actually applied in the Real World?


Thanks Danny.


Erik