How can I make a function equal to 0?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Manns

    How can I make a function equal to 0?

    Hi,

    Is there a way to create a function that is equal to 0?
    I try to redefine __cmp__ but I am pretty stuck.

    Something like:
    >>def f(): return ""
    ....
    >># Some magic
    >>f == 0
    True

    Thanks in advance

    Martin
  • Paul Rubin

    #2
    Re: How can I make a function equal to 0?

    Martin Manns <mmanns@gmx.net writes:
    Is there a way to create a function that is equal to 0?
    def f(): return 0

    Comment

    • Martin Manns

      #3
      Re: How can I make a function equal to 0?

      On 21 Mar 2008 12:52:12 -0700
      Paul Rubin <http://phr.cx@NOSPAM.i nvalidwrote:
      def f(): return 0
      Let us try it:

      Python 2.5.1 (r251:54863, Jan 26 2008, 01:34:00)
      [GCC 4.1.2 (Gentoo 4.1.2)] on linux2
      Type "help", "copyright" , "credits" or "license" for more information.
      >>def f(): return 0
      ....
      >>f==0
      False
      >>f()==0
      True
      >>>

      I do not want the function to return 0 but to equal 0.

      Martin

      Comment

      • attn.steven.kuo@gmail.com

        #4
        Re: How can I make a function equal to 0?

        On Mar 21, 12:48 pm, Martin Manns <mma...@gmx.net wrote:
        Hi,
        >
        Is there a way to create a function that is equal to 0?
        I try to redefine __cmp__ but I am pretty stuck.
        >
        Something like:
        >
        >def f(): return ""
        ...
        ># Some magic
        >f == 0
        >
        True
        >

        You would have to bind f (the name)
        to 0 (or False). You can "cheat"
        and use a decorator:
        >>def makezero(f): return 0
        >>@makezero
        .... def f(): return 1
        >>f == 0
        True

        --
        Hope this helps,
        Steven

        Comment

        • Ben Finney

          #5
          Re: How can I make a function equal to 0?

          Martin Manns <mmanns@gmx.net writes:
          I do not want the function to return 0 but to equal 0.
          Then it seems you don't want a function, but something else. Functions
          are functions, not integers.

          What problem are you trying to solve, and why do you think this
          behaviour will help?

          --
          \ "Pinky, are you pondering what I'm pondering?" "Well, I think |
          `\ so, Brain, but 'apply North Pole' to what?" -- _Pinky and The |
          _o__) Brain_ |
          Ben Finney

          Comment

          • John Machin

            #6
            Re: How can I make a function equal to 0?

            On Mar 22, 8:51 am, Gabriel Genellina <gagsl-...@yahoo.com.a rwrote:
            If you drop this condition then you could fill the array with zeroes
            as before, replace only the interesting ones with actual functions,
            and write:
            >
            for item in myarray[nonzero(myarray )]:
            print item() if callable(item) else item
            >
            Let's unbuckle the seat belt :-)

            If "item" is definitely restricted to being either 0 or a callable
            object, then
            item() if item else 0
            should give the same answer as, and is quite likely to be faster than,
            item() if callable(item) else item
            as it avoids a global lookup and a function call.

            Cheers,
            John

            Comment

            • Martin Manns

              #7
              Re: How can I make a function equal to 0?

              On Fri, 21 Mar 2008 14:51:49 -0700 (PDT)
              Gabriel Genellina <gagsl-py2@yahoo.com.a rwrote:
              If I fill the array with 0 instead of the functions that
              return "" this works really fast.

              However, I would like to be able call the content of each
              cell in the array as a function.
              >
              If you drop this condition then you could fill the array with zeroes
              as before, replace only the interesting ones with actual functions,
              and write:
              >
              for item in myarray[nonzero(myarray )]:
              print item() if callable(item) else item
              Works for me.

              Thank you

              Martin

              Comment

              • Gabriel Genellina

                #8
                Re: How can I make a function equal to 0?

                On 21 mar, 19:25, John Machin <sjmac...@lexic on.netwrote:
                On Mar 22, 8:51 am, Gabriel Genellina <gagsl-...@yahoo.com.a rwrote:
                >
                If you drop this condition then you could fill the array with zeroes
                as before, replace only the interesting ones with actual functions,
                and write:
                >
                for item in myarray[nonzero(myarray )]:
                    print item() if callable(item) else item
                >
                Let's unbuckle the seat belt :-)
                >
                If "item" is definitely restricted to being either 0 or a callable
                object, then
                    item() if item else 0
                should give the same answer as, and is quite likely to be faster than,
                    item() if callable(item) else item
                as it avoids a global lookup and a function call.
                In that case, we could use a bare item() because nonzero would have
                filtered out all that zeroes.

                --
                Gabriel Genellina

                Comment

                • John Machin

                  #9
                  Re: How can I make a function equal to 0?

                  On Mar 22, 3:32 pm, Gabriel Genellina <gagsl-...@yahoo.com.a rwrote:
                  On 21 mar, 19:25, John Machin <sjmac...@lexic on.netwrote:
                  >
                  >
                  >
                  On Mar 22, 8:51 am, Gabriel Genellina <gagsl-...@yahoo.com.a rwrote:
                  >
                  If you drop this condition then you could fill the array with zeroes
                  as before, replace only the interesting ones with actual functions,
                  and write:
                  >
                  for item in myarray[nonzero(myarray )]:
                  print item() if callable(item) else item
                  >
                  Let's unbuckle the seat belt :-)
                  >
                  If "item" is definitely restricted to being either 0 or a callable
                  object, then
                  item() if item else 0
                  should give the same answer as, and is quite likely to be faster than,
                  item() if callable(item) else item
                  as it avoids a global lookup and a function call.
                  >
                  In that case, we could use a bare item() because nonzero would have
                  filtered out all that zeroes.
                  True.

                  Comment

                  Working...