"is" and ==

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

    #1

    "is" and ==

    Can someone please explain to me the difference between the "is"
    keyword and the == boolean operator. I can't figure it out on my own
    and I can't find any documentation on it.

    I can't understand why this works:

    if text is None:

    and why this always returns false:

    if message is 'PING':

    even when message = 'PING'.

    What's the deal with that?

  • Erik Max Francis

    #2
    Re: "is&quo t; and ==

    BlueJ774 wrote:
    Can someone please explain to me the difference between the "is"
    keyword and the == boolean operator. I can't figure it out on my own
    and I can't find any documentation on it.
    >
    I can't understand why this works:
    >
    if text is None:
    >
    and why this always returns false:
    >
    if message is 'PING':
    >
    even when message = 'PING'.
    >
    What's the deal with that?
    `x is y` means the same thing as:

    id(x) == id(y)

    You use the `is` operator for when you're testing for _object identity_,
    not value. `None` is a special object sentinel that is not only a value
    but a special _object_, and so if you're testing whether or not an
    object is `None`, you do so with the `is` operator.

    If you're testing whether an object is equal to the string "PING" then
    you do not want to do so by identity, but rather value, so you use the
    `==` operator, not `is`.

    --
    Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
    San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
    You could have another fate / You could be in another place
    -- Anggun

    Comment

    • BlueJ774

      #3
      Re: "is&quo t; and ==

      On May 30, 12:57 am, Erik Max Francis <m...@alcyone.c omwrote:
      BlueJ774 wrote:
      Can someone please explain to me the difference between the "is"
      keyword and the == boolean operator. I can't figure it out on my own
      and I can't find any documentation on it.
      >
      I can't understand why this works:
      >
      if text is None:
      >
      and why this always returns false:
      >
      if message is 'PING':
      >
      even when message = 'PING'.
      >
      What's the deal with that?
      >
      `x is y` means the same thing as:
      >
      id(x) == id(y)
      >
      You use the `is` operator for when you're testing for _object identity_,
      not value. `None` is a special object sentinel that is not only a value
      but a special _object_, and so if you're testing whether or not an
      object is `None`, you do so with the `is` operator.
      >
      If you're testing whether an object is equal to the string "PING" then
      you do not want to do so by identity, but rather value, so you use the
      `==` operator, not `is`.
      >
      --
      Erik Max Francis && m...@alcyone.co m &&http://www.alcyone.com/max/
      San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
      You could have another fate / You could be in another place
      -- Anggun
      Thanks. That's exactly what I was looking for.

      Comment

      • Warren Stringer

        #4
        c[:]()

        I want to call every object in a tupple, like so:

        #------------------------------------------
        def a: print 'a'
        def b: print 'b'
        c = (a,b)
        >>>c[:]() # i wanna
        TypeError: 'tupple' object is not callable
        >>>c[0]() # expected
        a
        >>>c[:][0] # huh?
        a
        >>[i() for i in c] # too long and ...huh?
        a
        b
        [None,None]
        #------------------------------------------

        Why? Because I want to make Python calls from a cell phone.
        Every keystroke is precious; even list comprehension is too much.

        Is there something obvious that I'm missing?

        Warren

        Today's quote: "HELLO PARROT! wakey wakey!"

        Comment

        • Carsten Haese

          #5
          Re: c[:]()

          On Wed, 2007-05-30 at 11:48 -0700, Warren Stringer wrote:
          I want to call every object in a tupple, like so:
          >
          #------------------------------------------
          def a: print 'a'
          def b: print 'b'
          c = (a,b)
          >
          >>c[:]() # i wanna
          [...]
          Is there something obvious that I'm missing?
          Yes: Python is not Perl.

          Python is based on the principle that programmers don't write computer
          code for the benefit of the computer, but for the benefit of any
          programmer who has to read their code in the future. Terseness is not a
          virtue. To call every function in a tuple, do the obvious:

          for func in funcs: func()

          HTH,

          --
          Carsten Haese



          Comment

          • Warren Stringer

            #6
            RE: c[:]()

            Hmmm, this is for neither programmer nor computer; this is for a user. If I
            wanted to write code for the benefit for the computer, I'd still be flipping
            switches on a PDP-8. ;-)

            This is inconsistent:

            why does c[:][0]() work but c[:]() does not?
            Why does c[0]() has exactly the same results as c[:][0]() ?
            Moreover, c[:][0]() implies that a slice was invoked

            So, tell me, for scheduling a lot of asynchronous events, what would be more
            readable than this:

            bidders = [local_members] + [callin_members]
            bidders[:].sign_in(roster )
            ...

            \~/
            -----Original Message-----
            From: python-list-bounces+warren= muse.com@python .org [mailto:python-list-
            bounces+warren= muse.com@python .org] On Behalf Of Carsten Haese
            Sent: Wednesday, May 30, 2007 12:55 PM
            To: python-list@python.org
            Subject: Re: c[:]()
            >
            On Wed, 2007-05-30 at 11:48 -0700, Warren Stringer wrote:
            I want to call every object in a tupple, like so:

            #------------------------------------------
            def a: print 'a'
            def b: print 'b'
            c = (a,b)
            >>>c[:]() # i wanna
            [...]
            Is there something obvious that I'm missing?
            >
            Yes: Python is not Perl.
            >
            Python is based on the principle that programmers don't write computer
            code for the benefit of the computer, but for the benefit of any
            programmer who has to read their code in the future. Terseness is not a
            virtue. To call every function in a tuple, do the obvious:
            >
            for func in funcs: func()
            >
            HTH,
            >
            --
            Carsten Haese

            >
            >
            --
            http://mail.python.org/mailman/listinfo/python-list

            Comment

            • irstas@gmail.com

              #7
              Re: c[:]()

              On May 31, 12:31 am, "Warren Stringer" <war...@muse.co mwrote:
              This is inconsistent:
              >
              why does c[:][0]() work but c[:]() does not?
              Why does c[0]() has exactly the same results as c[:][0]() ?
              Moreover, c[:][0]() implies that a slice was invoked
              It's not inconsistent, but [:] probably does something different than
              you think it does. All it does is create a copy (not in general, but
              at least if c is a list or a tuple). Since in your example c is a
              tuple and tuples are immutable, making a copy of it is essentially
              useless. Why not just use the original? I.e. instead of c[:] you could
              just write c. That's why c[:][0]() has exactly the same effect as c[0]
              (), although the former is likely to be slightly slower.

              c[:]() tries to call the copied tuple. Tuples aren't callable.
              c[:][0]() calls the first element in the copied tuple, and that
              element happens to be callable.

              Comment

              • Brian van den Broek

                #8
                Re: c[:]()

                Warren Stringer said unto the world upon 05/30/2007 05:31 PM:
                Hmmm, this is for neither programmer nor computer; this is for a user. If I
                wanted to write code for the benefit for the computer, I'd still be flipping
                switches on a PDP-8. ;-)
                >
                This is inconsistent:
                >
                why does c[:][0]() work but c[:]() does not?
                c[:][0]() says take a copy of the list c, find its first element, and
                call it. Since c is a list of functions, that calls a function.

                c[:]() says take a copy of the list c and call it. Since lists are not
                callable, that doesn't work.
                Why does c[0]() has exactly the same results as c[:][0]() ?
                Because c[0] is equal to c[:][0].
                Moreover, c[:][0]() implies that a slice was invoked
                Yes, but the complete slice.
                So, tell me, for scheduling a lot of asynchronous events, what would be more
                readable than this:
                >
                bidders = [local_members] + [callin_members]
                bidders[:].sign_in(roster )
                ...
                for bidder in [local_members] + [callin_members]:
                bidder.sign_in( roster)

                Best,

                Brian vdB
                \~/
                >
                >-----Original Message-----
                >From: python-list-bounces+warren= muse.com@python .org [mailto:python-list-
                >bounces+warren =muse.com@pytho n.org] On Behalf Of Carsten Haese
                >Sent: Wednesday, May 30, 2007 12:55 PM
                >To: python-list@python.org
                >Subject: Re: c[:]()
                >>
                >On Wed, 2007-05-30 at 11:48 -0700, Warren Stringer wrote:
                >>I want to call every object in a tupple, like so:
                >>>
                >>#------------------------------------------
                >>def a: print 'a'
                >>def b: print 'b'
                >>c = (a,b)
                >>>
                >>>>>c[:]() # i wanna
                >>[...]
                >>Is there something obvious that I'm missing?
                >Yes: Python is not Perl.
                >>
                >Python is based on the principle that programmers don't write computer
                >code for the benefit of the computer, but for the benefit of any
                >programmer who has to read their code in the future. Terseness is not a
                >virtue. To call every function in a tuple, do the obvious:
                >>
                >for func in funcs: func()
                >>
                >HTH,
                >>
                >--
                >Carsten Haese
                >http://informixdb.sourceforge.net
                >>
                >>
                >--
                >http://mail.python.org/mailman/listinfo/python-list
                >
                >

                Comment

                • Warren Stringer

                  #9
                  RE: c[:]()

                  Hey many thanks for the replies!

                  Ah, so is seems that c[:][:][:][:][:][:][:][:][:][:][:][0]()
                  also work ...

                  Ah well, can't have everything. Guess I was inspired by the alphabetically
                  adjacent message "Call for Ruby Champion". Would have been nice for it work
                  - a more elegant iterator would be hard to come by. A PEP, perhaps? Maybe
                  not; am still a bit new - am probably missing something obvious why this
                  isn't an easy fix.

                  Pretty cool that I can override the list class.

                  Cheers,

                  \~/
                  -----Original Message-----
                  From: python-list-bounces+warren= muse.com@python .org [mailto:python-list-
                  bounces+warren= muse.com@python .org] On Behalf Of Brian van den Broek
                  Sent: Wednesday, May 30, 2007 3:00 PM
                  To: python-list@python.org
                  Subject: Re: c[:]()
                  >
                  Warren Stringer said unto the world upon 05/30/2007 05:31 PM:
                  Hmmm, this is for neither programmer nor computer; this is for a user.
                  If I
                  wanted to write code for the benefit for the computer, I'd still be
                  flipping
                  switches on a PDP-8. ;-)

                  This is inconsistent:

                  why does c[:][0]() work but c[:]() does not?
                  >
                  c[:][0]() says take a copy of the list c, find its first element, and
                  call it. Since c is a list of functions, that calls a function.
                  >
                  c[:]() says take a copy of the list c and call it. Since lists are not
                  callable, that doesn't work.
                  >
                  Why does c[0]() has exactly the same results as c[:][0]() ?
                  >
                  Because c[0] is equal to c[:][0].
                  >
                  Moreover, c[:][0]() implies that a slice was invoked
                  >
                  Yes, but the complete slice.
                  >
                  So, tell me, for scheduling a lot of asynchronous events, what would be
                  more
                  readable than this:

                  bidders = [local_members] + [callin_members]
                  bidders[:].sign_in(roster )
                  ...
                  >
                  for bidder in [local_members] + [callin_members]:
                  bidder.sign_in( roster)
                  >
                  Best,
                  >
                  Brian vdB
                  >
                  \~/
                  -----Original Message-----
                  From: python-list-bounces+warren= muse.com@python .org [mailto:python-
                  list-
                  bounces+warren= muse.com@python .org] On Behalf Of Carsten Haese
                  Sent: Wednesday, May 30, 2007 12:55 PM
                  To: python-list@python.org
                  Subject: Re: c[:]()
                  >
                  On Wed, 2007-05-30 at 11:48 -0700, Warren Stringer wrote:
                  >I want to call every object in a tupple, like so:
                  >>
                  >#------------------------------------------
                  >def a: print 'a'
                  >def b: print 'b'
                  >c = (a,b)
                  >>
                  >>>>c[:]() # i wanna
                  >[...]
                  >Is there something obvious that I'm missing?
                  Yes: Python is not Perl.
                  >
                  Python is based on the principle that programmers don't write computer
                  code for the benefit of the computer, but for the benefit of any
                  programmer who has to read their code in the future. Terseness is not a
                  virtue. To call every function in a tuple, do the obvious:
                  >
                  for func in funcs: func()
                  >
                  HTH,
                  >
                  --
                  Carsten Haese

                  >
                  >
                  --
                  http://mail.python.org/mailman/listinfo/python-list
                  >
                  --
                  http://mail.python.org/mailman/listinfo/python-list

                  Comment

                  • Dustan

                    #10
                    Re: c[:]()

                    On May 30, 5:37 pm, "Warren Stringer" <war...@muse.co mwrote:
                    Hey many thanks for the replies!
                    >
                    Ah, so is seems that c[:][:][:][:][:][:][:][:][:][:][:][0]()
                    also work ...
                    >
                    Ah well, can't have everything. Guess I was inspired by the alphabetically
                    adjacent message "Call for Ruby Champion". Would have been nice for it work
                    - a more elegant iterator would be hard to come by. A PEP, perhaps? Maybe
                    not; am still a bit new - am probably missing something obvious why this
                    isn't an easy fix.
                    >
                    Pretty cool that I can override the list class.
                    >
                    Cheers,
                    Do a search on "python is not java" (words to live by). You can also
                    plug in another language you know (like Ruby), but you won't get as
                    many results.

                    Comment

                    • Warren Stringer

                      #11
                      RE: c[:]()

                      Hey Dustan, very cool!

                      The first hit on "python is not java" turns up the dirtsimple blog, which
                      kicked my asp -- so, to speak (reptile not server pages - mon dieu, I'm
                      explain a pun, how wretched) ...

                      Dirtsimple mentions shallow versus deep. Damn!

                      First off, I love dots. They're easy to type. I like to go deep. Yet have
                      been finding that a.b.c.d.e.f() tends to suck __getattr__

                      The dirtsimple blog also mentions using hash-trees as a switch
                      statement...hmm mm .... I wonder which is faster:

                      a.b.c.d.e.f() # or
                      h['a.b.c.d.e.f']() # doh!

                      I still want my executable container though. Would love to so this

                      h[search('a//f')]() # where a//f is like an Xpath // search for leaves

                      Believe me, this is incredibly useful for executing an ontology, which
                      happens to be what I'm working on during every waking moment, when not on
                      the python list.

                      Cheers,

                      \~/
                      -----Original Message-----
                      From: python-list-bounces+warren= muse.com@python .org [mailto:python-list-
                      bounces+warren= muse.com@python .org] On Behalf Of Dustan
                      Sent: Wednesday, May 30, 2007 4:14 PM
                      To: python-list@python.org
                      Subject: Re: c[:]()
                      >
                      On May 30, 5:37 pm, "Warren Stringer" <war...@muse.co mwrote:
                      Hey many thanks for the replies!

                      Ah, so is seems that c[:][:][:][:][:][:][:][:][:][:][:][0]()
                      also work ...

                      Ah well, can't have everything. Guess I was inspired by the
                      alphabetically
                      adjacent message "Call for Ruby Champion". Would have been nice for it
                      work
                      - a more elegant iterator would be hard to come by. A PEP, perhaps?
                      Maybe
                      not; am still a bit new - am probably missing something obvious why this
                      isn't an easy fix.

                      Pretty cool that I can override the list class.

                      Cheers,
                      >
                      Do a search on "python is not java" (words to live by). You can also
                      plug in another language you know (like Ruby), but you won't get as
                      many results.
                      >
                      --
                      http://mail.python.org/mailman/listinfo/python-list

                      Comment

                      • Tijs

                        #12
                        Re: c[:]()

                        Warren Stringer wrote:
                        >>>>c[:]() # i wanna
                        TypeError: 'tupple' object is not callable
                        >
                        c[:] equals c (in expressions), so c[:]() is equivalent to c()
                        >>>>c[:][0] # huh?
                        a
                        c[:][0] is c[0] is a
                        >>>[i() for i in c] # too long and ...huh?
                        a
                        b
                        [None,None]
                        #------------------------------------------
                        >
                        [None, None] is the result of the operation.

                        for i in c: i()

                        If that is too long, reconsider what you are doing.

                        --

                        Regards,
                        Tijs

                        Comment

                        • Warren Stringer

                          #13
                          RE: c[:]()

                          Oops! guess I should have tested my rather hasty complaint about executable
                          containers. This is nice:

                          def a(): return 'b'
                          def b(): print 'polly! wakey wakey'
                          c = {}
                          c['a'] = b
                          c[a()]() #works!


                          c[a()]() is a switch statement with an amorphous selector- very handy in its
                          own right. But, using a() as a generator would be more expressive. This
                          seems to work:

                          c = [a,a]
                          [d() for d in c]

                          But that still isn't as simple or as direct as:

                          c[:]()

                          Which would you rather explain to a 12-year-old writing code for the first
                          time?
                          I still want my executable container though. Would love to so this
                          >
                          h[search('a//f')]() # where a//f is like an Xpath // search for leaves
                          >

                          Comment

                          • Duncan Booth

                            #14
                            Re: c[:]()

                            irstas@gmail.co m wrote:
                            On May 31, 12:31 am, "Warren Stringer" <war...@muse.co mwrote:
                            >This is inconsistent:
                            >>
                            >why does c[:][0]() work but c[:]() does not?
                            >Why does c[0]() has exactly the same results as c[:][0]() ?
                            >Moreover, c[:][0]() implies that a slice was invoked
                            >
                            It's not inconsistent, but [:] probably does something different than
                            you think it does. All it does is create a copy (not in general, but
                            at least if c is a list or a tuple). Since in your example c is a
                            tuple and tuples are immutable, making a copy of it is essentially
                            useless. Why not just use the original? I.e. instead of c[:] you could
                            just write c. That's why c[:][0]() has exactly the same effect as c[0]
                            (), although the former is likely to be slightly slower.
                            An extremely minor point (and implementation specific), but while [:] will
                            copy a list it won't copy a tuple. Likewise 'list(aList)' will always copy
                            a list, 'tuple(aTuple)' won't copy a tuple. So in this particular example
                            the slice is completely redundant.
                            >>c is c[:] is c[:][:][:][:] is tuple(c)
                            True

                            Comment

                            • Douglas Woodrow

                              #15
                              Re: c[:]()

                              On Wed, 30 May 2007 23:23:22, Warren Stringer <warren@muse.co mwrote
                              >
                              >def a(): return 'b'
                              >def b(): print 'polly! wakey wakey'
                              >c = {}
                              >c['a'] = b
                              >c[a()]() #works!

                              (typo correction for other easily-confused newbies like myself)

                              I think you mean
                              ,----
                              | c['a']() #works!
                              `----

                              --
                              Doug Woodrow

                              Comment

                              Working...