RE: Thoughts on new vs traditional idioms

Discussion in 'Python' started by Delaney, Timothy C (Timothy), Mar 1, 2004.

  1. > From: Peter Otten
    >
    > Not particular frequent, but anyway:
    >
    > >>> sample = [[1,2], (3,4)]
    > >>> copies = [seq.__class__(seq) for seq in sample]
    > >>> for s, c in zip(sample, copies):

    > ... if s is c: print s
    > ...
    > (3, 4)
    >
    > Assuming all immutable classes are "smart enough":


    That's a *very* big assumption - one I would never make.

    It is explicitly stated that immutable objects *may* be reused. For some functions and methods, it states that they *will* be reused in certain situations. Only in those cases can or should you rely on that behaviour.

    Tim Delaney
     
    Delaney, Timothy C (Timothy), Mar 1, 2004
    #1
    1. Advertising

  2. Delaney, Timothy C (Timothy)

    Peter Otten Guest

    Delaney, Timothy C (Timothy) wrote:

    [unrelated code snipped]

    >> Assuming all immutable classes are "smart enough":


    [... to reuse an instance of the exact class passed to their copy
    constructor - to use C++ terminology for lack of any better]

    > That's a *very* big assumption - one I would never make.


    Do you have a class with immutable instances handy where the following
    assertion fails?

    assert immutableObj.__class__(immutableObj) is immutableObj

    Peter
     
    Peter Otten, Mar 2, 2004
    #2
    1. Advertising

  3. Delaney, Timothy C (Timothy)

    Jeff Epler Guest

    On Tue, Mar 02, 2004 at 03:48:19PM +0100, Peter Otten wrote:
    > Do you have a class with immutable instances handy where the following
    > assertion fails?
    >
    > assert immutableObj.__class__(immutableObj) is immutableObj


    Python 2.3.2 (#1, Oct 17 2003, 10:41:07)
    >>> import sets
    >>> ii = sets.ImmutableSet([1,2,3])
    >>> ii.__class__(ii) is ii

    False

    Jeff
     
    Jeff Epler, Mar 2, 2004
    #3
  4. Delaney, Timothy C (Timothy)

    Peter Otten Guest

    Jeff Epler wrote:

    > On Tue, Mar 02, 2004 at 03:48:19PM +0100, Peter Otten wrote:
    >> Do you have a class with immutable instances handy where the following
    >> assertion fails?
    >>
    >> assert immutableObj.__class__(immutableObj) is immutableObj

    >
    > Python 2.3.2 (#1, Oct 17 2003, 10:41:07)
    >>>> import sets
    >>>> ii = sets.ImmutableSet([1,2,3])
    >>>> ii.__class__(ii) is ii

    > False


    You win :)

    Now I think about it (again), the above is the behaviour to be expected for
    every immutable class (at least if it's implemented in Python, don't know
    about C) that predates the advent of __new__().

    By the way, is ImmutableSet meant to stay? In the 1.4 setobject.c code there
    is a frozenset which _has_ the "do I really need to copy" check:

    if (iterable != NULL && PyFrozenSet_CheckExact(iterable)) {
    Py_INCREF(iterable);
    return iterable;
    }

    Peter
     
    Peter Otten, Mar 2, 2004
    #4
    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. Rob Thomas
    Replies:
    3
    Views:
    387
  2. Jan
    Replies:
    2
    Views:
    1,028
    Jan Pastrnak
    Nov 3, 2003
  3. Raymond Hettinger

    Thoughts on new vs traditional idioms

    Raymond Hettinger, Feb 29, 2004, in forum: Python
    Replies:
    7
    Views:
    357
    Michele Simionato
    Mar 1, 2004
  4. Ben Charrow

    Idioms and Anti-Idioms Question

    Ben Charrow, Jun 22, 2009, in forum: Python
    Replies:
    11
    Views:
    551
    Lawrence D'Oliveiro
    Jul 4, 2009
  5. Rui Maciel
    Replies:
    1
    Views:
    304
    Pavel
    May 28, 2012
Loading...

Share This Page