0

Hello i have a weird doubt..

MY code is

    def prints():
        print " I WILL DIE HEHE"

    def add(a,b):
        next = a
        print " Added sum is %d " % (next + b)
        next = prints()


    w = int(raw_input("Give a"))
    g = int(raw_input("Give b"))
    add(w,g)

Now the problem is why does the function prints() gets executed while i assign it to next i.e. next = prints(). I am little confused.

1
  • What did you expect to happen when you assigned prints() to next? Commented Jan 21, 2013 at 22:36

2 Answers 2

6

Because you are calling it,

prints()

will execute where as

ne = prints
ne() 

is assigning the name, and then calling the new name.

Noteback, I also call it ne as next shadows a builtin method

Sign up to request clarification or add additional context in comments.

Comments

2

The following:

next = prints()

calls prints(), and assigns the result to next. Since prints() doesn't explicitly return anything, it implicitly returns None, and thus next is set to None.

It is not entirely clear what you're expecting or trying to achieve, but it may be helpful to consider the following:

next = prints

What this does is assign the function object to next. It does not call the function, but allows it to be called as next().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.