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

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

  1. On 19May2013 11:11, Chris Angelico <> wrote:
    | On Sun, May 19, 2013 at 10:26 AM, Cameron Simpson <> wrote:
    | > 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...)
    |
    | Why is it an int subclass? Because there are places where you want to
    | use it as though it were an int? It might be easier to render those,
    | instead, eg by creating a __int__ method. (Or is it "an __int__
    | method"? Not sure.)

    I don't want to use it as an int, on the outside. I want to use an
    int on the inside as the implementation.

    It's an int _subclass_ so that it is no bigger than an int. Otherwise
    I may as well just make an ordinary object and be back where I
    started. Bulky:-(

    The reason it is an _int_ subclass, versus something else, is that
    a bitmap is a type of int. So the functional mapping is direct.

    I _do_ _not_ want to operate on it from the outside as an int (doing
    overt addition, for example, though I can imagine doing bitwise
    activities); I want to operate on in _internally_ as a int to decide
    what names-by-an-attribute flags are on or off.

    So an object with an __int__() method is right out; it's the wrong
    interface because it would mean an int() call (possibly implicit)
    in exterior code.

    Cheers,
    --
    Cameron Simpson <>

    Hoping to shave precious seconds off the time it would take me to get
    through the checkout process and on my way home, I opted for the express
    line ("9 Items Or Less [sic]" Why nine items? Where do they come up with
    these rules, anyway? It's the same way at most stores -- always some
    oddball number like that, instead of a more understandable multiple of
    five. Like "five.")
    - Geoff Miller,
     
    Cameron Simpson, May 19, 2013
    #1
    1. Advertising

  2. Cameron Simpson wrote:
    > It's an int _subclass_ so that it is no bigger than an int.


    If you use __slots__ to eliminate the overhead of an
    instance dict, you'll get an object consisting of a
    header plus one reference, which is probably about the
    size of an int. But you'll also need an int to put in
    that slot, so the total size will be about twice that
    of an int.

    Another approach would be to subclass array.array and
    give instances of it type integer and size 1. Together
    with empty __slots__, it will probably be a bit bigger
    than an int, but it might still be smaller than a
    custom object plus an int.

    If all of these are still too big, you might need to
    find some way of packing multiple instances into a
    single array.array.

    --
    Greg
     
    Gregory Ewing, May 20, 2013
    #2
    1. Advertising

  3. On 20May2013 13:23, Greg Ewing <> wrote:
    | Cameron Simpson wrote:
    | >It's an int _subclass_ so that it is no bigger than an int.
    |
    | If you use __slots__ to eliminate the overhead of an
    | instance dict, you'll get an object consisting of a
    | header plus one reference, which is probably about the
    | size of an int. But you'll also need an int to put in
    | that slot, so the total size will be about twice that
    | of an int.

    Yeah, I was thinking I'd need to go that way. Thanks for the
    suggestion.

    | Another approach would be to subclass array.array and
    | give instances of it type integer and size 1. Together
    | with empty __slots__, it will probably be a bit bigger
    | than an int, but it might still be smaller than a
    | custom object plus an int.

    Really? Interesting. I thinik it crosses my "too baroque" threshold,
    but maybe not:)

    | If all of these are still too big, you might need to
    | find some way of packing multiple instances into a
    | single array.array.

    Space isn't that real an issue at present; I'll keep that kind of
    approach in mind if it comes up. This really came up because I was
    feeling that the obvious object-with-boolean-attributes was terrbily
    wasteful for something that can be inplemented with a single int,
    in principle.

    Cheers,
    --
    Cameron Simpson <>

    >>>How do you blip the throttle and wave? Do you blip it real high, then wave
    >>>before the revs drop back?

    >>Blip = right hand; Wave = left hand. Do both simultaneously. QED.

    >Doesnt this make the bike lurch forward thru the intersection?

    Not if the disk lock is in place...
    - Dean Woodward <>
     
    Cameron Simpson, May 20, 2013
    #3
    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:
    871
    Jack Klein
    Jul 10, 2004
  2. Cameron Simpson
    Replies:
    0
    Views:
    121
    Cameron Simpson
    May 19, 2013
  3. Chris Angelico
    Replies:
    0
    Views:
    116
    Chris Angelico
    May 19, 2013
  4. Peter Otten
    Replies:
    0
    Views:
    126
    Peter Otten
    May 19, 2013
  5. Cameron Simpson
    Replies:
    0
    Views:
    96
    Cameron Simpson
    May 20, 2013
Loading...

Share This Page