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

Discussion in 'Python' started by Cameron Simpson, May 20, 2013.

  1. On 19May2013 09:01, Peter Otten <> 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 <>

    Carpe Daemon - Seize the Background Process
    - Paul Tomblin <>
    Cameron Simpson, May 20, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Skybuck Flying

    ints ints ints and ints

    Skybuck Flying, Jul 8, 2004, in forum: C Programming
    Replies:
    24
    Views:
    820
    Jack Klein
    Jul 10, 2004
  2. Cameron Simpson
    Replies:
    0
    Views:
    101
    Cameron Simpson
    May 19, 2013
  3. Chris Angelico
    Replies:
    0
    Views:
    96
    Chris Angelico
    May 19, 2013
  4. Cameron Simpson
    Replies:
    2
    Views:
    104
    Cameron Simpson
    May 20, 2013
  5. Peter Otten
    Replies:
    0
    Views:
    108
    Peter Otten
    May 19, 2013
Loading...

Share This Page