Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #80243 > unrolled thread

What is the difference between these two? (Assigning functions to variables)

Started byMax Nathaniel Ho <maxhowl86@gmail.com>
First post2014-11-04 19:15 -0800
Last post2014-11-04 23:33 -0800
Articles 4 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  What is the difference between these two? (Assigning functions to variables) Max Nathaniel Ho <maxhowl86@gmail.com> - 2014-11-04 19:15 -0800
    Re: What is the difference between these two? (Assigning functions to variables) Max Nathaniel Ho <maxhowl86@gmail.com> - 2014-11-04 19:17 -0800
      Re: What is the difference between these two? (Assigning functions to variables) Cameron Simpson <cs@zip.com.au> - 2014-11-05 16:59 +1100
        Re: What is the difference between these two? (Assigning functions to variables) Max Nathaniel Ho <maxhowl86@gmail.com> - 2014-11-04 23:33 -0800

#80243 — What is the difference between these two? (Assigning functions to variables)

FromMax Nathaniel Ho <maxhowl86@gmail.com>
Date2014-11-04 19:15 -0800
SubjectWhat is the difference between these two? (Assigning functions to variables)
Message-ID<79948f2a-df75-4161-8b78-450a0ba9f16b@googlegroups.com>
Example 1

def compose_greet_func():
    def get_message():
        return "Hello there!"

    return get_message

greet = compose_greet_func()
print greet()


Example 2 

def greet(name):
    return "hello "+name

greet_someone = greet
print greet_someone("John"

In Example 1, the function compoe_greet_func is assigned to the variable greet, and () is included at the end of the function.

However, in Example 2, the function greet is assigned to the variable greet_someone but () is excluded at the end of the function.

Does the () matter when assigning functions to variables? 

Thank you!

[toc] | [next] | [standalone]


#80244

FromMax Nathaniel Ho <maxhowl86@gmail.com>
Date2014-11-04 19:17 -0800
Message-ID<3de3eb9f-f0ae-494d-973f-0b9829af54d7@googlegroups.com>
In reply to#80243
Just to be clear, I was referring to these two lines

greet = compose_greet_func() 

greet_someone = greet 

On Wednesday, November 5, 2014 11:15:46 AM UTC+8, Max Nathaniel Ho wrote:
> Example 1
> 
> def compose_greet_func():
>     def get_message():
>         return "Hello there!"
> 
>     return get_message
> 
> greet = compose_greet_func()
> print greet()
> 
> 
> Example 2 
> 
> def greet(name):
>     return "hello "+name
> 
> greet_someone = greet
> print greet_someone("John"
> 
> In Example 1, the function compoe_greet_func is assigned to the variable greet, and () is included at the end of the function.
> 
> However, in Example 2, the function greet is assigned to the variable greet_someone but () is excluded at the end of the function.
> 
> Does the () matter when assigning functions to variables? 
> 
> Thank you!

[toc] | [prev] | [next] | [standalone]


#80246

FromCameron Simpson <cs@zip.com.au>
Date2014-11-05 16:59 +1100
Message-ID<mailman.15477.1415167196.18130.python-list@python.org>
In reply to#80244
On 04Nov2014 19:17, Max Nathaniel Ho <maxhowl86@gmail.com> wrote:
>Just to be clear, I was referring to these two lines
>
>greet = compose_greet_func()
>
>greet_someone = greet

Please don't top-post. Thanks.

Your first assignment:

   greet = compose_greet_func()

_calls_ (runs) the compose_greet_func and assigns its return value to greet.

Your second assignment:

   greet_someone = greet

assigns the current value of "greet", whatever that is, to "greet_someone". No 
function is called.

Cheers,
Cameron Simpson <cs@zip.com.au>

[toc] | [prev] | [next] | [standalone]


#80252

FromMax Nathaniel Ho <maxhowl86@gmail.com>
Date2014-11-04 23:33 -0800
Message-ID<c0183806-cb7a-48bb-989f-42baa3177f63@googlegroups.com>
In reply to#80246
On Wednesday, November 5, 2014 2:00:08 PM UTC+8, Cameron Simpson wrote:
> On 04Nov2014 19:17, Max Nathaniel Ho <maxhowl86@gmail.com> wrote:
> >Just to be clear, I was referring to these two lines
> >
> >greet = compose_greet_func()
> >
> >greet_someone = greet
> 
> Please don't top-post. Thanks.
> 
> Your first assignment:
> 
>    greet = compose_greet_func()
> 
> _calls_ (runs) the compose_greet_func and assigns its return value to greet.
> 
> Your second assignment:
> 
>    greet_someone = greet
> 
> assigns the current value of "greet", whatever that is, to "greet_someone". No 
> function is called.
> 
> Cheers,
> Cameron Simpson <cs@zip.com.au>

Apologies about the top posting.

Ahh okay, thank you for your explanation

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web