Re: Puzzled by "is"

Discussion in 'Python' started by =?UTF-8?B?R3J6ZWdvcnogU8WCb2Rrb3dpY3o=?=, Aug 9, 2007.

  1. >
    > Why? Because.
    >
    > Seriously, it's just an optimization by the implementers. There is no
    > need for more than one empty tuple, since tuples can never be modified
    > once created.
    >
    > But they decided not to create (1, ) in advance. They probably knew that
    > hardly anybody would want to create that tuple ;-) [Seriously: if you
    > started trying to predict which tuples would be used you would go
    > insane, but the empty tuple is the most likely candidate].
    >

    That's just theorisation but I'd rather expect the interpreter simply
    not to create a second tuple while there already is an identical one.
    This could save some memory if the tuple was large (Although by the same
    token comparison of large tuples can be expensive). Admittedly the empty
    tuple is a special case but then 'Special cases aren't special enough to
    break the rules'.

    A bit odd.

    Best regards,
    Greg
    =?UTF-8?B?R3J6ZWdvcnogU8WCb2Rrb3dpY3o=?=, Aug 9, 2007
    #1
    1. Advertising

  2. Grzegorz SÅ‚odkowicz wrote:

    > That's just theorisation but I'd rather expect the interpreter simply
    > not to create a second tuple while there already is an identical one.
    > This could save some memory if the tuple was large (Although by the same
    > token comparison of large tuples can be expensive). Admittedly the empty
    > tuple is a special case but then 'Special cases aren't special enough to
    > break the rules'.
    >
    > A bit odd.


    It doesn't save time if you have to check through all the existing
    tuples for matches ...

    --
    Erik Max Francis && && http://www.alcyone.com/max/
    San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
    Chance favors the trained mind.
    -- Louis Pasteur
    Erik Max Francis, Aug 9, 2007
    #2
    1. Advertising

  3. Grzegorz S³odkowicz <> writes:

    >> Seriously, it's just an optimization by the implementers. There is
    >> no need for more than one empty tuple, since tuples can never be
    >> modified once created.
    >>
    >> But they decided not to create (1, ) in advance. They probably knew
    >> that hardly anybody would want to create that tuple ;-) [Seriously:
    >> if you started trying to predict which tuples would be used you
    >> would go insane, but the empty tuple is the most likely candidate].
    >>

    > That's just theorisation but I'd rather expect the interpreter simply
    > not to create a second tuple while there already is an identical
    > one.


    But then tuple creation would be slowed down by searching for whether
    an "identical one" already exists. In the general case, that is quite
    unlikely, so it's not done. (I suspect that only the requirement to
    store the list of all tuples somewhere would outweigh any potential
    gains of this strategy; and if the search were implemented as a hash
    table lookup, even more space would be wasted.) It's done for the
    empty tuple because no search is necessary, only a size test.

    > Admittedly the empty tuple is a special case but then 'Special cases
    > aren't special enough to break the rules'.


    Except no rule is being broken. As others have pointed out, since
    tuples are immutable, caching them is quite safe.
    Hrvoje Niksic, Aug 9, 2007
    #3
  4. =?UTF-8?B?R3J6ZWdvcnogU8WCb2Rrb3dpY3o=?=

    Ben Finney Guest

    Grzegorz SÅ‚odkowicz <> writes:

    > That's just theorisation but I'd rather expect the interpreter
    > simply not to create a second tuple while there already is an
    > identical one.


    Others have already said that it's an implementation optimisation,
    which seems to partly answer your question.

    It's important to also realise that the language is *deliberately*
    non-committal on whether any given value will have this behaviour;
    that is, it's entirely left to the language implementation which
    optimisation trade-offs to make, and the language user (that's you and
    I) should *not* expect any particular behaviour to hold between
    different implementations.

    --
    \ "Holy priceless collection of Etruscan snoods, Batman!" -- |
    `\ Robin |
    _o__) |
    Ben Finney
    Ben Finney, Aug 10, 2007
    #4
  5. Ben Finney wrote:

    > It's important to also realise that the language is *deliberately*
    > non-committal on whether any given value will have this behaviour;
    > that is, it's entirely left to the language implementation which
    > optimisation trade-offs to make, and the language user (that's you and
    > I) should *not* expect any particular behaviour to hold between
    > different implementations.


    Right. In the terminology of language standards, it is implementation
    defined.

    --
    Erik Max Francis && && http://www.alcyone.com/max/
    San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
    Only love is worth the risk
    -- Oleta Adams
    Erik Max Francis, Aug 10, 2007
    #5
  6. =?UTF-8?B?R3J6ZWdvcnogU8WCb2Rrb3dpY3o=?=

    Dick Moores Guest

    At 06:13 PM 8/9/2007, Ben Finney wrote:
    >Content-Transfer-Encoding: base64Grzegorz
    >SÅ‚odkowicz <Üš]\΂‚ˆ] ÜÈ@st
    >theorisation but I'd rather expect the interpreter
    > > simply not to create a second tuple while there already is an
    > > identical one.

    >
    >Others have already said that it's an implementation optimisation,
    >which seems to partly answer your question.
    >
    >It's important to also realise that the language is *deliberately*
    >non-committal on whether any given value will have this behaviour;
    >that is, it's entirely left to the language implementation which
    >optimisation trade-offs to make, and the language user (that's you and
    >I) should *not* expect any particular behaviour to hold between
    >different implementations.


    I'm not clear on the meaning of "implementations"
    here. Would 2.5 for Windows, Mac, Linux all be
    different implementations? Would Iron Python be another? ActivePython?

    Thanks,

    Dick
    Dick Moores, Aug 10, 2007
    #6
  7. On Fri, 10 Aug 2007 10:57:22 -0700, Dick Moores wrote:

    > At 06:13 PM 8/9/2007, Ben Finney wrote:
    >>Others have already said that it's an implementation optimisation,
    >>which seems to partly answer your question.
    >>
    >>It's important to also realise that the language is *deliberately*
    >>non-committal on whether any given value will have this behaviour;
    >>that is, it's entirely left to the language implementation which
    >>optimisation trade-offs to make, and the language user (that's you and
    >>I) should *not* expect any particular behaviour to hold between
    >>different implementations.

    >
    > I'm not clear on the meaning of "implementations"
    > here. Would 2.5 for Windows, Mac, Linux all be
    > different implementations? Would Iron Python be another? ActivePython?


    Taken to the extreme you shouldn't rely on implementation details at all,
    so even the very same interpreter might cache and reuse the empty tuple in
    one run but not in the next.

    For me CPython, Iron Python and Jython are different implementations as
    they are all written from scratch.

    Ciao,
    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, Aug 10, 2007
    #7
  8. =?UTF-8?B?R3J6ZWdvcnogU8WCb2Rrb3dpY3o=?=

    Trent Mick Guest

    Dick Moores wrote:
    > At 06:13 PM 8/9/2007, Ben Finney wrote:
    >> It's important to also realise that the language is *deliberately*
    >> non-committal on whether any given value will have this behaviour;
    >> that is, it's entirely left to the language implementation which
    >> optimisation trade-offs to make, and the language user (that's you and
    >> I) should *not* expect any particular behaviour to hold between
    >> different implementations.

    >
    > I'm not clear on the meaning of "implementations"
    > here. Would 2.5 for Windows, Mac, Linux all be
    > different implementations? Would Iron Python be another? ActivePython?


    (Note that I'm jumping into this without having read the thread, so
    apologies if I didn't grasp the question in its proper context.)

    Separate Python "implementations" generally refers to a separate source
    code base for that Python build. According to that definition, current
    examples of Python implementations are: CPython (from python.org,
    C-based), Jython (Java-based), Stackless Python (I'm not that familiar
    to how much of the CPython source code, if any, Stackless borrows),
    IronPython (.NET/C#-based), possibly PyPy (Python-based, I'm not that
    familiar with it). Also eventually interesting might be the recently
    announced IronMonkey (http://wiki.mozilla.org/Tamarin:IronMonkey).

    Windows, Mac, Linux, etc. installations/builds/installers of a
    particular implementation would be different platform *builds* (my
    language).

    ActivePython is a separate (from python.org's) *distribution* of CPython
    -- i.e. the sample implementation as CPython (same source code). This is
    similar, in some respects, to SuSE, Debian, RedHat, Ubuntu, etc. being
    different distributions of Linux.


    Trent

    --
    Trent Mick
    trentm at activestate.com
    Trent Mick, Aug 10, 2007
    #8
  9. =?UTF-8?B?R3J6ZWdvcnogU8WCb2Rrb3dpY3o=?=

    Ben Finney Guest

    Dick Moores <> writes:

    > At 06:13 PM 8/9/2007, Ben Finney wrote:
    > >it's entirely left to the language implementation which
    > >optimisation trade-offs to make, and the language user (that's you
    > >and I) should *not* expect any particular behaviour to hold between
    > >different implementations.

    >
    > I'm not clear on the meaning of "implementations" here. Would 2.5
    > for Windows, Mac, Linux all be different implementations? Would Iron
    > Python be another? ActivePython?


    For the purpose of the above statement, you should consider even the
    same Python on two different machines to be "different
    implementations". As a programmer writing Python code, you should not
    expect any "implementation-dependent" behaviour to operate in any
    particular way.

    --
    \ "The fact that a believer is happier than a skeptic is no more |
    `\ to the point than the fact that a drunken man is happier than a |
    _o__) sober one." —George Bernard Shaw |
    Ben Finney
    Ben Finney, Aug 12, 2007
    #9
  10. =?UTF-8?B?R3J6ZWdvcnogU8WCb2Rrb3dpY3o=?=

    Steve Holden Guest

    Dick Moores wrote:
    > On 8/12/07, *Ben Finney* <
    > <mailto:>> wrote:
    >
    > Dick Moores < <mailto:>> writes:
    >
    > > At 06:13 PM 8/9/2007, Ben Finney wrote:
    > > >it's entirely left to the language implementation which
    > > >optimisation trade-offs to make, and the language user (that's you
    > > >and I) should *not* expect any particular behaviour to hold between
    > > >different implementations.

    > >
    > > I'm not clear on the meaning of "implementations" here. Would 2.5
    > > for Windows, Mac, Linux all be different implementations? Would Iron
    > > Python be another? ActivePython?

    >
    > For the purpose of the above statement, you should consider even the
    > same Python on two different machines to be "different
    > implementations". As a programmer writing Python code, you should not
    > expect any "implementation-dependent" behaviour to operate in any
    > particular way.
    >
    >
    > So would a programmer EVER use "is" in a script?


    Sure. For example, the canonical test for None uses

    x is None

    because there is only ever one instance of type Nonetype, so it's the
    fastest test. Generally speaking you use "is" to test for identity (do
    these two expressions reference the same object) rather than equality
    (do these two expressions evaluate to equivalent objects).

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    --------------- Asciimercial ------------------
    Get on the web: Blog, lens and tag the Internet
    Many services currently offer free registration
    ----------- Thank You for Reading -------------
    Steve Holden, Aug 12, 2007
    #10
  11. =?UTF-8?B?R3J6ZWdvcnogU8WCb2Rrb3dpY3o=?=

    Dick Moores Guest

    At 08:23 AM 8/12/2007, Steve Holden wrote:
    >Dick Moores wrote:
    > > So would a programmer EVER use "is" in a script?

    >
    >Sure. For example, the canonical test for None uses
    >
    > x is None
    >
    >because there is only ever one instance of type Nonetype, so it's the
    >fastest test. Generally speaking you use "is" to test for identity (do
    >these two expressions reference the same object) rather than equality
    >(do these two expressions evaluate to equivalent objects).


    Off the top of your head, could you or others give me as many
    examples as you can think of?

    Thanks again,

    Dick
    Dick Moores, Aug 12, 2007
    #11
  12. =?UTF-8?B?R3J6ZWdvcnogU8WCb2Rrb3dpY3o=?=

    Steve Holden Guest

    Dick Moores wrote:
    > At 08:23 AM 8/12/2007, Steve Holden wrote:
    >> Dick Moores wrote:
    >>> So would a programmer EVER use "is" in a script?

    >> Sure. For example, the canonical test for None uses
    >>
    >> x is None
    >>
    >> because there is only ever one instance of type Nonetype, so it's the
    >> fastest test. Generally speaking you use "is" to test for identity (do
    >> these two expressions reference the same object) rather than equality
    >> (do these two expressions evaluate to equivalent objects).

    >
    > Off the top of your head, could you or others give me as many
    > examples as you can think of?
    >

    Occasionally it's necessary to test for a specific type (though in
    Python this is usually bad practice). Since types are also singletons
    the best way to do this is (e.g.):

    type(x) is type([]) # test specifically for a list

    If you want to know whether you have been told to write to standard
    output, one possible test is

    if f is not sys.stdout

    Similarly, of course, you can test for the other standard IO channels.

    The imputil module contains the test

    if importer is not self

    to determine whether a reload() should be performed in the context of
    the current package.

    When you need to establish a specific sentinel value that can never be
    provided by an outside caller it's normal to create an instance of
    object (the simplest possible thing you can create in a Python program)
    and test for that instance, as in

    sentinel = object()
    ...
    if value is sentinel:

    You can test whether a class is new-style as opposed to old-style, which
    can help to unify old-style and new-style objects:

    class MetaProperty(type):
    def __new__(cls, name, bases, dct):
    if bases[0] is object: # allow us to create class Property
    return type.__new__(cls, name, bases, dct)
    return property(dct.get('get'), dct.get('set'),
    dct.get('delete'), dct.get('__doc__'))

    def __init__(cls, name, bases, dct):
    if bases[0] is object:
    return type.__init__(cls, name, bases, dct)


    That gets you started ...

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    --------------- Asciimercial ------------------
    Get on the web: Blog, lens and tag the Internet
    Many services currently offer free registration
    ----------- Thank You for Reading -------------
    Steve Holden, Aug 12, 2007
    #12
  13. =?UTF-8?B?R3J6ZWdvcnogU8WCb2Rrb3dpY3o=?=

    Dick Moores Guest

    At 09:59 AM 8/12/2007, Steve Holden wrote:
    >Dick Moores wrote:
    > > At 08:23 AM 8/12/2007, Steve Holden wrote:
    > >> Dick Moores wrote:
    > >>> So would a programmer EVER use "is" in a script?
    > >> Sure. For example, the canonical test for None uses
    > >>
    > >> x is None
    > >>
    > >> because there is only ever one instance of type Nonetype, so it's the
    > >> fastest test. Generally speaking you use "is" to test for identity (do
    > >> these two expressions reference the same object) rather than equality
    > >> (do these two expressions evaluate to equivalent objects).

    > >
    > > Off the top of your head, could you or others give me as many
    > > examples as you can think of?
    > >

    >Occasionally it's necessary to test for a specific type (though in
    >Python this is usually bad practice). Since types are also singletons
    >the best way to do this is (e.g.):
    >
    > type(x) is type([]) # test specifically for a list
    >
    >If you want to know whether you have been told to write to standard
    >output, one possible test is
    >
    > if f is not sys.stdout
    >
    >Similarly, of course, you can test for the other standard IO channels.
    >
    >The imputil module contains the test
    >
    > if importer is not self
    >
    >to determine whether a reload() should be performed in the context of
    >the current package.
    >
    >When you need to establish a specific sentinel value that can never be
    >provided by an outside caller it's normal to create an instance of
    >object (the simplest possible thing you can create in a Python program)
    >and test for that instance, as in
    >
    > sentinel = object()
    > ...
    > if value is sentinel:
    >
    >You can test whether a class is new-style as opposed to old-style, which
    >can help to unify old-style and new-style objects:
    >
    >class MetaProperty(type):
    > def __new__(cls, name, bases, dct):
    > if bases[0] is object: # allow us to create class Property
    > return type.__new__(cls, name, bases, dct)
    > return property(dct.get('get'), dct.get('set'),
    > dct.get('delete'), dct.get('__doc__'))
    >
    > def __init__(cls, name, bases, dct):
    > if bases[0] is object:
    > return type.__init__(cls, name, bases, dct)
    >
    >
    >That gets you started ...


    Sure does. Thanks very much, Steve.

    Dick
    Dick Moores, Aug 12, 2007
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Andrew

    VHDL: puzzled beginner

    Mark Andrew, Jun 27, 2004, in forum: VHDL
    Replies:
    5
    Views:
    640
    Mark Andrew
    Jun 30, 2004
  2. Daniel

    still puzzled

    Daniel, Jun 8, 2004, in forum: ASP .Net
    Replies:
    11
    Views:
    644
    Steven Cheng[MSFT]
    Jun 9, 2004
  3. schiefaw

    Very Puzzled, Please Help

    schiefaw, Jul 22, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    372
    schiefaw
    Jul 22, 2005
  4. Replies:
    5
    Views:
    1,397
  5. EvelynAnd Ethan
    Replies:
    4
    Views:
    975
    =?Utf-8?B?Sko=?=
    Jan 9, 2006
Loading...

Share This Page