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


Groups > comp.lang.python > #45554

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

Path csiph.com!usenet.pasdenom.info!gegeweb.org!newsfeed.kamp.net!newsfeed.kamp.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'essentially': 0.04; 'attribute': 0.07; 'modify': 0.07; 'attributes': 0.09; 'compact': 0.09; 'iterate': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:into': 0.09; 'toss': 0.09; 'mostly': 0.14; '"if"': 0.16; '"in': 0.16; 'math,': 0.16; 'nick': 0.16; 'object()': 0.16; 'partly': 0.16; 'readability': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'sense,': 0.16; 'simpson': 0.16; 'subclass': 0.16; 'wrote:': 0.18; 'all,': 0.19; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'fine': 0.24; 'options': 0.25; 'this:': 0.26; 'header:X-Complaints-To:1': 0.27; "doesn't": 0.30; 'statement': 0.30; 'work.': 0.31; '(which': 0.31; 'code': 0.31; 'that.': 0.31; '"",': 0.31; 'flags': 0.31; 'work:': 0.31; 'yesterday': 0.31; 'anyone': 0.31; 'file': 0.32; '(most': 0.33; 'could': 0.34; 'problem.': 0.35; 'no,': 0.35; 'objects': 0.35; 'but': 0.35; 'are,': 0.36; 'false': 0.36; 'object,': 0.36; 'should': 0.36; 'to:addr:python-list': 0.38; 'recent': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'access,': 0.60; 'break': 0.61; 'forward': 0.65; '(that': 0.65; 'default': 0.69; 'subject:have': 0.80; "'object'": 0.84; 'bare': 0.84; 'liking': 0.84; 'subject:think': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: mutable ints: I think I have painted myself into a corner
Date Sun, 19 May 2013 09:01:49 +0200
Organization None
References <20130519002629.GA14025@cskk.homeip.net>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084bc5c.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
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.1838.1368946921.3114.python-list@python.org> (permalink)
Lines 62
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1368946921 news.xs4all.nl 15871 [2001:888:2000:d::a6]:50270
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:45554

Show key headers only | View raw


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:

>>> object().this = True
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute 'this'

> The use case for flags is essentially boolean/binary, and so a int
> accessed as a bitmask should be smaller.
> 
> So I pulled out my BitMask int subclass (which mostly transcribes
> the int as "A|B|C" for readability purposes, partly to dillute Nick
> Coglan's liking for bulky strings over compact ints on readability
> grounds:-), and gave the subclass attribute access.
> 
> This works just fine for querying the flags object, with code exactly
> like the "if" statement above.
> 
> 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)

work.

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


Thread

Re: mutable ints: I think I have painted myself into a corner Peter Otten <__peter__@web.de> - 2013-05-19 09:01 +0200

csiph-web