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


Groups > comp.lang.python > #45540

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

Date 2013-05-19 10:26 +1000
From Cameron Simpson <cs@zip.com.au>
Subject mutable ints: I think I have painted myself into a corner
Newsgroups comp.lang.python
Message-ID <mailman.1826.1368925069.3114.python-list@python.org> (permalink)

Show all headers | View raw


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!

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...)

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

Why does "philosophy of consciousness/nature of reality" seem to interest you
so much?  Take away consciousness and reality and there's not much left.
- Greg Egan, interview in Eidolon 15

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


Thread

mutable ints: I think I have painted myself into a corner Cameron Simpson <cs@zip.com.au> - 2013-05-19 10:26 +1000

csiph-web