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


Groups > comp.lang.python > #45586

Re: mutable ints: I think I have painted myself into a corner

Path csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <cameron@cskk.homeip.net>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'broken': 0.04; 'essentially': 0.04; 'modify': 0.07; 'modifying': 0.07; 'attributes': 0.09; 'iterate': 0.09; 'subject:into': 0.09; 'toss': 0.09; 'yeah,': 0.09; 'cc:addr:python-list': 0.11; "wouldn't": 0.14; '"in': 0.16; 'factory': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'math,': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'object()': 0.16; 'received:211.29': 0.16; 'received:211.29.132': 0.16; 'received:optusnet.com.au': 0.16; 'received:syd.optusnet.com.au': 0.16; 'sense,': 0.16; 'simpson': 0.16; 'subclass': 0.16; 'to:addr:web.de': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'cc:addr:python.org': 0.22; 'header:User-Agent:1': 0.23; 'paul': 0.24; 'cheers,': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'options': 0.25; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; "doesn't": 0.30; 'work.': 0.31; 'code': 0.31; 'that.': 0.31; 'flags': 0.31; 'int,': 0.31; 'object.': 0.31; 'work:': 0.31; 'yesterday': 0.31; 'anyone': 0.31; 'could': 0.34; 'problem.': 0.35; 'computing': 0.35; 'no,': 0.35; 'objects': 0.35; 'but': 0.35; 'are,': 0.36; 'false': 0.36; 'received:com.au': 0.36; 'returning': 0.36; 'charset:us-ascii': 0.36; 'received:211': 0.38; 'does': 0.39; 'access,': 0.60; 'break': 0.61; 'new': 0.61; 'content-disposition:inline': 0.62; 'forward': 0.65; '(that': 0.65; 'default': 0.69; 'subject:have': 0.80; 'bare': 0.84; 'otten': 0.84; 'subject:think': 0.84; 'sorry.': 0.91
Date Mon, 20 May 2013 15:57:51 +1000
From Cameron Simpson <cs@zip.com.au>
To Peter Otten <__peter__@web.de>
Subject Re: mutable ints: I think I have painted myself into a corner
MIME-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Disposition inline
In-Reply-To <kn9tck$k6u$1@ger.gmane.org>
User-Agent Mutt/1.5.21 (2010-09-15)
References <kn9tck$k6u$1@ger.gmane.org>
X-Optus-CM-Score 0
X-Optus-CM-Analysis v=2.0 cv=e4Ne0tV/ c=1 sm=1 a=wom5GMh1gUkA:10 a=21x4_w1R3MUA:10 a=kj9zAlcOel0A:10 a=vrnE16BAAAAA:8 a=ZtCCktOnAAAA:8 a=StZfE6sK-9QA:10 a=bxj_ZKTTsZDJFd2x8g4A:9 a=CjuIK1q_8ugA:10 a=5KVauyKsRKMA:10 a=ChdAjXE5lkUvdteQbhpnkQ==:117
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1859.1369029912.3114.python-list@python.org> (permalink)
Lines 59
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1369029912 news.xs4all.nl 15964 [2001:888:2000:d::a6]:57685
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:45586

Show key headers only | View raw


On 19May2013 09:01, Peter Otten <__peter__@web.de> wrote:
| Cameron Simpson wrote:
| 
| > TL;DR: I think I want to modify an int value "in place".
| > 
| > Yesterday I was thinking about various "flag set" objects I have
| > floating around which are essentially bare "object"s whose attributes
| > I access, for example:
| > 
| >   flags = object()
| >   flags.this = True
| >   flags.that = False
| > 
| > and then elsewhere:
| > 
| >   if flags.that:
| >     do that ...
| > 
| > Nice and readable, but I thought to myself: so bulky!
| 
| Plus, it doesn't work:

Yeah, sorry. My "old" code was a trite subclass of object.
The new broken code is a subclass of int.

| > But setting up a flags object? What I _want_ to write is code like this:
| > 
| >   Flags = BitMask('this', 'that')
| > 
| >   # set default state
| >   flags = Flags()
| >   flags.this = False
| >   flags.that = True
| >   ... iterate over some options ...: flags.this = True
| > 
| > and there's my problem. This would modify the int in place. There's
| > no way to do that. For the base type (int) this makes perfect sense,
| > as they're immutable.
| > 
| > Before I toss this approach and retreat to my former "object"
| > technique, does anyone see a way forward to modify an int subclass
| > instance in place? (That doesn't break math, preferably; I don't
| > do arithmetic with these things but they are, after all, ints...)
| 
| No, but you could make
| 
| flags = Flags(this=False, that=True)

Yes, that is true. Flags would need to be a factory function computing
the corresponding bitmask value and then returning my new int, but
it would work. It still wouldn't let me go the whole way of modifying
the flags after instantiation.

Cheers,
-- 
Cameron Simpson <cs@zip.com.au>

Carpe Daemon - Seize the Background Process
        - Paul Tomblin <ab401@freenet2.carleton.ca>

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: mutable ints: I think I have painted myself into a corner Cameron Simpson <cs@zip.com.au> - 2013-05-20 15:57 +1000

csiph-web