Re: Easy immutability in python?

Discussion in 'Python' started by Terry Hancock, Mar 4, 2006.

  1. Whoops I forgot to list the reference.
    Also, I just finished reading the old thread, so I see some
    damage-control may be needed.

    On Sat, 4 Mar 2006 11:52:59 -0600
    Terry Hancock <> wrote:
    > [1]
    > http://news.hping.org/comp.lang.python.archive/28916.html


    And more specifically, I'm referring to Alex Martelli's
    reply:

    http://news.hping.org/comp.lang.python.archive/28966.html

    [Vain attempt to co-opt a flame-war follows]

    And having seen that that discussion largely devolved into a
    rant about why "you shouldn't do that", can we PLEASE
    restrict this thread to the *how*?

    My use case is quite similar to Ben's enumeration type. In
    both cases, assigning attributes to the objects is
    NONSENSICAL and possibly UNWISE. Any attempt to do so is
    almost certainly a bug in your code if you are using my
    module -- and it happens to be one I want to make sure I
    catch. It is also true that the average person using my
    code is not a blackbelt pythoneer, but rather a scripter who
    needs/wants training wheels whenever it is practical to
    provide them.

    If you are REALLY sure you want mutability, you can always
    subclass it.

    If you think that code that handles prime numbers by marking
    integer objects like so:

    101.prime = True

    is "just fine", and are not bothered that 101 is
    mysteriously unclassified sometime after you've marked it
    (because 100+1 is 101 == False), then we are not going to
    see eye-to-eye, so let's not argue about it.

    I also am not trying to alter the Python language. I am
    trying to figure out how to most easily fix __setattr__ etc
    to act immutably, *using* the existing features.

    I can already do what I want with some 25-30 lines of code
    repeated each time I need it, but I think it's dumb that
    it's that complicated.

    --
    Terry Hancock ()
    Anansi Spaceworks http://www.AnansiSpaceworks.com
     
    Terry Hancock, Mar 4, 2006
    #1
    1. Advertising

  2. Terry Hancock

    Guest

    I guess we think a bit differently, and we think about different
    problems. When I hear, "immutable container", I think "tuple". When I
    hear, "my own class that is an immutable container", I think, "subclass
    tuple, and probably override __new__ because otherwise tuple would be
    good enough as is".

    I'm not sure how this relates to the clp thread that you cite. I
    didn't read the whole thing, but I didn't find it to be a flamewar so
    much as a typical clp contest of tedium, which failed to devolve into a
    flamewar simply due to the maturity of the interlocutors. To
    summarize: first post is a use case, second post is an implementation
    of that use case, and subsequent posts alternate between "that's not
    how I want to do it" and "please provide a more specific use case for
    which the provided implementation is not acceptable".

    good luck,
    Jess
     
    , Mar 4, 2006
    #2
    1. Advertising

  3. Terry Hancock <> wrote:
    ...
    > I also am not trying to alter the Python language. I am
    > trying to figure out how to most easily fix __setattr__ etc
    > to act immutably, *using* the existing features.
    >
    > I can already do what I want with some 25-30 lines of code
    > repeated each time I need it, but I think it's dumb that
    > it's that complicated.


    If you need many immutable classes, inject __setattr__ etc into them
    (via metaclass or otherwise).


    Alex
     
    Alex Martelli, Mar 4, 2006
    #3
    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. Terry Hancock

    Easy immutability in python?

    Terry Hancock, Mar 4, 2006, in forum: Python
    Replies:
    3
    Views:
    394
    Terry Hancock
    Mar 4, 2006
  2. Roy Smith

    Python String Immutability Broken!

    Roy Smith, Aug 24, 2008, in forum: Python
    Replies:
    2
    Views:
    303
    Patrick Maupin
    Aug 25, 2008
  3. John Nagle

    The future of Python immutability

    John Nagle, Sep 3, 2009, in forum: Python
    Replies:
    50
    Views:
    1,261
    Steven D'Aprano
    Sep 8, 2009
  4. andrea crotti

    Immutability and Python

    andrea crotti, Oct 29, 2012, in forum: Python
    Replies:
    8
    Views:
    222
    Thomas Rachel
    Nov 8, 2012
  5. andrea crotti

    Re: Immutability and Python

    andrea crotti, Oct 29, 2012, in forum: Python
    Replies:
    0
    Views:
    176
    andrea crotti
    Oct 29, 2012
Loading...

Share This Page