Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32419 > unrolled thread

Re: Immutability and Python

Started byChris Angelico <rosuav@gmail.com>
First post2012-10-30 06:36 +1100
Last post2012-10-29 22:34 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Immutability and Python Chris Angelico <rosuav@gmail.com> - 2012-10-30 06:36 +1100
    Re: Immutability and Python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-29 22:34 +0000

#32419 — Re: Immutability and Python

FromChris Angelico <rosuav@gmail.com>
Date2012-10-30 06:36 +1100
SubjectRe: Immutability and Python
Message-ID<mailman.3044.1351539415.27098.python-list@python.org>
On Tue, Oct 30, 2012 at 6:23 AM, Ian Kelly <ian.g.kelly@gmail.com> 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

[toc] | [next] | [standalone]


#32431

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-10-29 22:34 +0000
Message-ID<508f0475$0$29967$c3e8da3$5496439d@news.astraweb.com>
In reply to#32419
On Tue, 30 Oct 2012 06:36:52 +1100, Chris Angelico wrote:

> On Tue, Oct 30, 2012 at 6:23 AM, Ian Kelly <ian.g.kelly@gmail.com>
> 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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web