function/method assigment

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • viscroad@gmail.com

    #1

    function/method assigment

    I have a confusion when I do some practice, the code and output are as
    following,
    >>def fun():
    print 'In fun()....'

    >>testfun = fun()
    In fun()....
    >>print testfun
    None
    >>testfun2 = fun
    >>print testfun2
    <function fun at 0x00CC1270>
    >>print testfun2()
    In fun()....
    None
    >>>
    what is 'testfun'? Why it is 'None'? And print testfun2(), what is the
    meaning of 'None'?

    Thanks!

  • Steve Holden

    #2
    Re: function/method assigment

    viscroad@gmail. com wrote:
    I have a confusion when I do some practice, the code and output are as
    following,
    >
    >>>def fun():
    print 'In fun()....'
    >
    >
    >>>testfun = fun()
    In fun()....
    >>>print testfun
    None
    >>>testfun2 = fun
    >>>print testfun2
    <function fun at 0x00CC1270>
    >>>print testfun2()
    In fun()....
    None
    >
    what is 'testfun'? Why it is 'None'? And print testfun2(), what is the
    meaning of 'None'?
    >
    Thanks!
    >
    >
    When a function does not specifically return a value then its return
    value is a particular value known as None, the only instance of the None
    type.

    So testfun is the result of calling the fun function, and it's None
    because fun() does not return a value.

    Since testfun2 is just another reference to the fun function, testfun2()
    is None for exactly the same reasons.

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    Recent Ramblings http://holdenweb.blogspot.com

    Comment

    • Jakub Stolarski

      #3
      Re: function/method assigment

      On Apr 13, 6:14 pm, viscr...@gmail. com wrote:
      I have a confusion when I do some practice, the code and output are as
      following,
      >
      >def fun():
      >
      print 'In fun()....'
      >
      >testfun = fun()
      In fun()....
      >print testfun
      None
      >testfun2 = fun
      >print testfun2
      >
      <function fun at 0x00CC1270>
      >print testfun2()
      >
      In fun()....
      None
      >
      >
      >
      what is 'testfun'? Why it is 'None'? And print testfun2(), what is the
      meaning of 'None'?
      >
      Thanks!
      Your 'fun' is the sam as:

      def fun():
      print 'In fun()....'
      return None

      So
      testfun = fun()

      First prints message and then assign None to testfun.

      Comment

      • Bruno Desthuilliers

        #4
        Re: function/method assigment

        viscroad@gmail. com a ¨¦crit :
        I have a confusion when I do some practice, the code and output are as
        following,
        >
        >>>def fun():
        print 'In fun()....'
        Function fun doesn't explicitelly return something, so it's return value
        defaults to None (nb: None is a builtin object representing 'nothing').
        >
        >>>testfun = fun()
        This calls fun, and binds the returned value (in this case, None) to
        name testfun
        In fun()....
        and this is a side-effect of the execution of function fun.
        >>>print testfun
        None
        Which is the expected result
        >>>testfun2 = fun
        This binds the function object fun to the name testfun2 (IOW, testfun2
        is now an alias for fun). Remember that in Python, the parens are not
        optional if you want to call a function - they are in fact the 'call
        operator'. If you forget them, you get a reference to the function
        object, not the result of calling the function.
        >>>print testfun2
        This prints the representation of the object bound to name testfun2 -
        here, function fun.
        <function fun at 0x00CC1270>
        >>>print testfun2()
        In fun()....
        None
        This first calls testfun2 (which is now another name for function fun),
        triggering the printing of the string "in fun()..." as a side effects,
        then print the return value of testfun2 (aka fun), which is still None.

        HTH

        Comment

        • 7stud

          #5
          Re: function/method assigment

          On Apr 13, 10:14 am, viscr...@gmail. com wrote:
          I have a confusion when I do some practice, the code and output are as
          following,
          >
          >def fun():
          >
          print 'In fun()....'
          >
          >testfun = fun()
          In fun()....
          >print testfun
          None
          >testfun2 = fun
          >print testfun2
          >
          <function fun at 0x00CC1270>>>pr int testfun2()
          >
          In fun()....
          None
          >
          >
          >
          what is 'testfun'? Why it is 'None'? And print testfun2(), what is the
          meaning of 'None'?
          >
          Thanks!
          Start with these rules:

          1) all function calls in your code are replaced by the function's
          return value.

          2) if a function doesn't have a return statement, it automatically
          returns None.

          3 The '()' symbols make the function execute. Without those the
          function won't execute.


          Comment

          • 7stud

            #6
            Re: function/method assigment

            On Apr 13, 10:14 am, viscr...@gmail. com wrote:
            what is the
            meaning of 'None'?
            >
            It's a value just like any other python value: 2, 7.5, "red", and it
            evaluates to false in a conditional:

            my_var = None

            if not my_var:
            print "bad data"

            Comment

            Working...