Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:Python': 0.05; 'arguments': 0.07; 'collections': 0.09; 'immutable': 0.09; 'methods,': 0.09; 'subclass': 0.09; 'def': 0.10; 'namedtuple': 0.16; 'namedtuples': 0.16; 'oct': 0.16; 'mon,': 0.16; 'wrote:': 0.17; 'python?': 0.20; 'import': 0.21; 'cheers,': 0.23; 'header :In-Reply-To:1': 0.25; 'skip:m 30': 0.26; 'am,': 0.27; 'message- id:@mail.gmail.com': 0.27; 'skip:_ 10': 0.29; 'class': 0.29; 'this.': 0.29; 'received:209.85.215.46': 0.30; 'function.': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'sometimes': 0.35; 'received:209.85': 0.35; 'received:209': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'skip:n 30': 0.69; 'andrea': 0.84; 'doi': 0.84; 'to:name:python': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=nrmfETeSUlBmcxLe2fRh6pRTB8aoNMt9En+toX59jg8=; b=BvXMZLWSQXod+0G2k0XNBO8EjmNwPvkDyy0nacywsJfJVaSH9ej3fvdPXO3+WFMxXw F6nVjZ+ezXR21tX4ZnkO5mdd6bFbc3wWU5at8QmJMEZDuoGjnpVyM4n2dkKVGzVEj/+u 0g1rnKBhmdMZf626YnBvX1uPJqkVyPqZthpfrbjCMRpV4xYk8yiUcQC03atSLwmBqW1j H6gbJdKWRaEdvm49ZiFvlLHTXMj+sJnu+fdsot7l8xfdHYSsAChf5Hug3w/B7xYgRsBI S9gCBPZx7TRbi2ivfjHsIPlEErme0i5MR/vjvJtBSkrQuAto4LUd49YotCnm8d/f2A5S eOQA== MIME-Version: 1.0 In-Reply-To: References: <1793477354.3492917.1351526431192.JavaMail.root@sequans.com> From: Ian Kelly Date: Mon, 29 Oct 2012 13:23:03 -0600 Subject: Re: Immutability and Python To: Python Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 26 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351538616 news.xs4all.nl 6905 [2001:888:2000:d::a6]:36314 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32418 On Mon, Oct 29, 2012 at 10:12 AM, andrea crotti wrote: > Also because how doi I make an immutable object in pure Python? I sometimes use namedtuples for this. from collections import namedtuple MyImmutableClass = namedtuple('MyImmutableClass', 'field1 field2 field3 field4') If you want default arguments then use a factory function. Or if you want the class to have methods, then subclass it: _MyImmutableClass = namedtuple('MyImmutableClass', 'field1 field2 field3 field4') class MyImmutableClass(_MyImmutableClass): def __new__(cls, field1, field2, field3=None, field4=42): return super().__new__(cls, field1, field2, field3, field4) def get_sum(self): return self.field1 + self.field2 Cheers, Ian