Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'raises': 0.07; '(none,': 0.09; 'creation,': 0.09; 'descr': 0.09; 'immutable': 0.09; 'cc:addr:python-list': 0.10; 'python.': 0.11; '"this': 0.13; 'exception': 0.13; 'def': 0.14; 'disallow': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'immutable,': 0.16; 'mylist': 0.16; 'wrote:': 0.16; '>>>': 0.20; '(or': 0.21; 'cc:2**0': 0.21; 'cc:addr:python.org': 0.21; '(by': 0.22; '2015': 0.23; 'sat,': 0.23; 'this:': 0.23; 'header:In-Reply- To:1': 0.24; 'raise': 0.24; 'error': 0.27; 'message- id:@mail.gmail.com': 0.28; 'looks': 0.29; 'class': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'subject:?': 0.34; 'received:google.com': 0.34; 'could': 0.35; 'something': 0.35; 'but': 0.36; 'subject:: ': 0.37; 'list.': 0.37; 'skip:- 20': 0.37; 'moment': 0.38; 'self': 0.38; 'pm,': 0.39; 'does': 0.39; 'close': 0.61; 'construction': 0.73; '**kw)': 0.84; '**kw):': 0.84; 'chrisa': 0.84; 'to:none': 0.90; 'happen:': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=cqHTcUq+bF57mCl4MVNKfB+5EQYYinKjksngqpVdfXo=; b=ITnzdqsP40mVbDPEAScBInEBpKoUEhd3QqFxGZ3gqs7GK7xlLD0IzcOPU/viqTF1Ad i8CZ7oEWYfmK/MMfcHtm8Tc4Zxkpu4hz6XlCiDoE76CN/y7mYMdaNssF9zbLJcVcKlb/ +IkLdSNSycR3S+HYGiQnL7Y+SeHpCSPfxOqb/zkvzbDhEzCZGDwmXv2SppYbqmENcGhy ov/Bp4GKR0/c04+vFEIiKE7Ytin3VUlFVleUUb42Htyst8hHRZnmp2fPL+JUg6Lh2iuk iAl7D3ieXfWcf+DahrY8EdRJesP0nkE6+jfDsU0z/9vrWcxqBNoLVZzsd/JILDLLvJdJ NADw== MIME-Version: 1.0 X-Received: by 10.42.154.65 with SMTP id p1mr13899985icw.26.1433568548848; Fri, 05 Jun 2015 22:29:08 -0700 (PDT) In-Reply-To: <55727c1c$0$13000$c3e8da3$5496439d@news.astraweb.com> References: <3bbe49da-e989-4a8c-a8a9-75d3a786f508@googlegroups.com> <557056f9$0$13009$c3e8da3$5496439d@news.astraweb.com> <5570ce43$0$12991$c3e8da3$5496439d@news.astraweb.com> <55710b69$0$12980$c3e8da3$5496439d@news.astraweb.com> <6c78b294-efd6-4096-a572-1841aa71a5eb@googlegroups.com> <557182af$0$13014$c3e8da3$5496439d@news.astraweb.com> <33a2f501-5587-4fd9-91fa-7094d4f6c512@googlegroups.com> <81cb1232-ecf8-4cf0-b29c-ecf2ca47ade0@googlegroups.com> <55727c1c$0$13000$c3e8da3$5496439d@news.astraweb.com> Date: Sat, 6 Jun 2015 15:29:08 +1000 Subject: Re: Can Python function return multiple data? From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1433568557 news.xs4all.nl 2929 [2001:888:2000:d::a6]:57684 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:92171 On Sat, Jun 6, 2015 at 2:50 PM, Steven D'Aprano wrote: > This does not happen: > > mylist = [] > mytuple = (None, 1, mylist) > mylist.append(0) > => raises an exception > > The *tuple* is immutable, not the list. What you could have is a "FrozenList" (by analogy with frozenset), something like this: class FrozenList(tuple): def __new__(*a, **kw): self = tuple.__new__(*a, **kw) hash(self) # If error, disallow construction return self That would raise the error at the moment of mytuple's creation, not at mylist's mutation, but that's about as close as I can think of to a "this truly must be immutable" object in Python. And of course, any object can lie (or be mistaken) about its hashability and mutability: >>> def adder(x, inc=1): return x+inc ... >>> hash(adder) -9223363254790714706 >>> FrozenList((adder,)) (,) >>> descr = {adder: "Add 1 to a number"} Looks immutable to me. >>> adder.__defaults__=2, >>> next(iter(descr))(6) 8 Oops. ChrisA