[Tutor] Function object

Denis Gomes denisg640 at gmail.com
Thu Aug 26 03:00:29 CEST 2010


Daniel,

     Considering that Python is your first programming language, let's start
from the absolute beginning.  Before you think about what a function object
is, try to understand what a function is.  A function is a series of
python commands put together under a common heading or name in order to
achieve a specific task.  A function can receive parameters from the main
program, perform some operations using the parameters and return value(s) to
the main program.  In the following example, add is a function and it, like
all other functions is defined using the def statement:

def add(x,y):
     sum=x+y    # python commands here
     return sum

The x and y are parameters or arguments that you pass to the function
add.  x and y can be anything depending on what you want your function to
do.  In our particular example x and y will be numbers (assuming, integers)
because the add function will take the parameters, adds them together and
temporarily save the result in a variable called sum.  The return statement
will then take the result of the addition and send it back to the main
program.  When this happens the sum variable you created inside the function
will go out of scope (i.e. you will not be able to refer to it any more).
To use the function above say you have the following snippet in a file
called foo.py:

------------------------foo.py--------------------
def add(x,y):
     sum=x+y
     return sum

if __name__=="__main__":    # Execution of code starts here when you type
">>> python foo.py".  Python knows you defined add above.  Think of this as
the main program.
     result=add(1,1)    # You are calling add and storing the value returned
into a variable called result
     print result    # Assuming you are using Python2.X
----------------------------------------------------

Type
>>> python foo.py

Run it and see what happens.  You should get the answer 2.  From the main
program, you are calling the function add and storing the value that it
returns into a variable called result.  Then you are printing that value to
screen.

Once you get to learning about classes you will get a better understanding
of why a function in python is an object but I think you should try to get
the mechanics down first.

Experiment! Good luck.

Denis




On Wed, Aug 25, 2010 at 2:44 PM, Alan Gauld <alan.gauld at btinternet.com>wrote:

>
> "Daniel" <asmosis.asterix at gmail.com> wrote
>
>
> another problem regarding understanding a concept- function object. As I
>> said, I do not understand what a function object is, what it does, and
>> what
>> can I do with it?
>>
>
> You are actually using them all the time.
> Every function in Python is a function object.
> You can execute the function by putting parenthesesc after its name(filled
> with any required arguments)
>
> def foo(): return None
>
> defines a function object called foo that does nothing but return None.
>
> We can rewrite that using the lambda operator which returns function
> objects with no name:
>
> foo = lambda : None
>
> This assigns an anonymous function object to a variable foo - thus giving
> it a name, just like any other variable.
>
> In both cases we can now call the function with:
>
> foo()
>
> The advantage of treating functions as objects is that we can store them
> and then call them later. This is the basis of event driven programming
> and GUIs. It also allows us to easily build reconfigurable dynamic logic
> into applications - just by changing the order in which functions are
> called.
>
> It also allows us to create functions which return functions as their
> output,
> but this is getting into deeper water, so I'll ignore that for now! :-)
>
> You will find a slightly different explanation in the Functional
> Programming
> topic of my tutorial
>
>
> HTH
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
> _______________________________________________
>   Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100825/8c89a85a/attachment.html>


More information about the Tutor mailing list