Re: Immutability and Python

Discussion in 'Python' started by Chris Angelico, Oct 29, 2012.

  1. On Tue, Oct 30, 2012 at 6:23 AM, Ian Kelly <> wrote:
    > _MyImmutableClass = namedtuple('MyImmutableClass', 'field1 field2
    > field3 field4')
    >
    > class MyImmutableClass(_MyImmutableClass):


    Question: Is it clearer to take advantage of the fact that the base
    class can be an arbitrary expression?

    class MyImmutableClass(namedtuple('MyImmutableClass', 'field1 field2
    field3 field4')):

    You lose the unnecessary temporary and triplication of name, but gain
    instead a rather long line.

    ChrisA
     
    Chris Angelico, Oct 29, 2012
    #1
    1. Advertisements

  2. On Tue, 30 Oct 2012 06:36:52 +1100, Chris Angelico wrote:

    > On Tue, Oct 30, 2012 at 6:23 AM, Ian Kelly <>
    > wrote:
    >> _MyImmutableClass = namedtuple('MyImmutableClass', 'field1 field2
    >> field3 field4')
    >>
    >> class MyImmutableClass(_MyImmutableClass):

    >
    > Question: Is it clearer to take advantage of the fact that the base
    > class can be an arbitrary expression?
    >
    > class MyImmutableClass(namedtuple('MyImmutableClass', 'field1 field2
    > field3 field4')):



    I'm too lazy to google for it, but if you read the examples provided by
    namedtuple's creator, Raymond Hettinger, that is precisely one of the
    styles he uses. No need to explicitly declare the base class before using
    it.



    --
    Steven
     
    Steven D'Aprano, Oct 29, 2012
    #2
    1. Advertisements

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:
    543
    Terry Hancock
    Mar 4, 2006
  2. Terry Hancock

    Re: Easy immutability in python?

    Terry Hancock, Mar 4, 2006, in forum: Python
    Replies:
    2
    Views:
    400
    Alex Martelli
    Mar 4, 2006
  3. Nick Maclaren

    Immutability

    Nick Maclaren, Jun 28, 2006, in forum: Python
    Replies:
    37
    Views:
    1,120
  4. Roy Smith

    Python String Immutability Broken!

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

    The future of Python immutability

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

    Immutability and Python

    andrea crotti, Oct 29, 2012, in forum: Python
    Replies:
    8
    Views:
    421
    Thomas Rachel
    Nov 8, 2012
  7. Paul Rubin

    Re: Immutability and Python

    Paul Rubin, Oct 29, 2012, in forum: Python
    Replies:
    7
    Views:
    311
    Steven D'Aprano
    Oct 29, 2012
  8. Eric Jacoboni

    Tuples and immutability

    Eric Jacoboni, Feb 27, 2014, in forum: Python
    Replies:
    145
    Views:
    1,392
    Ethan Furman
    Mar 19, 2014
Loading...