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


Groups > comp.lang.python > #45542 > unrolled thread

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

Started byCameron Simpson <cs@zip.com.au>
First post2013-05-19 11:54 +1000
Last post2013-05-20 16:07 +1000
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: mutable ints: I think I have painted myself into a corner Cameron Simpson <cs@zip.com.au> - 2013-05-19 11:54 +1000
    Re: mutable ints: I think I have painted myself into a corner Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2013-05-20 13:23 +1200
      Re: mutable ints: I think I have painted myself into a corner Cameron Simpson <cs@zip.com.au> - 2013-05-20 16:07 +1000

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

FromCameron Simpson <cs@zip.com.au>
Date2013-05-19 11:54 +1000
SubjectRe: mutable ints: I think I have painted myself into a corner
Message-ID<mailman.1828.1368928471.3114.python-list@python.org>
On 19May2013 11:11, Chris Angelico <rosuav@gmail.com> wrote:
| On Sun, May 19, 2013 at 10:26 AM, Cameron Simpson <cs@zip.com.au> 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 <cs@zip.com.au>

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, geoffm@purplehaze.Corp.Sun.COM

[toc] | [next] | [standalone]


#45578

FromGregory Ewing <greg.ewing@canterbury.ac.nz>
Date2013-05-20 13:23 +1200
Message-ID<avtc8iFp5vU1@mid.individual.net>
In reply to#45542
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

[toc] | [prev] | [next] | [standalone]


#45587

FromCameron Simpson <cs@zip.com.au>
Date2013-05-20 16:07 +1000
Message-ID<mailman.1860.1369030093.3114.python-list@python.org>
In reply to#45578
On 20May2013 13:23, Greg Ewing <greg.ewing@canterbury.ac.nz> 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 <cs@zip.com.au>

>>>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 <deanw@agora.rdrop.com>

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web