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. Advertising

  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. 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. andrea crotti

    Immutability and Python

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

    Re: Immutability and Python

    andrea crotti, Oct 29, 2012, in forum: Python
    Replies:
    0
    Views:
    166
    andrea crotti
    Oct 29, 2012
  3. andrea crotti

    Re: Immutability and Python

    andrea crotti, Oct 29, 2012, in forum: Python
    Replies:
    0
    Views:
    162
    andrea crotti
    Oct 29, 2012
  4. Mark Lawrence

    Re: Immutability and Python

    Mark Lawrence, Oct 29, 2012, in forum: Python
    Replies:
    0
    Views:
    173
    Mark Lawrence
    Oct 29, 2012
  5. Paul Rubin

    Re: Immutability and Python

    Paul Rubin, Oct 29, 2012, in forum: Python
    Replies:
    7
    Views:
    148
    Steven D'Aprano
    Oct 29, 2012
Loading...

Share This Page