[Python-ideas] int()

Steven D'Aprano steve at pearwood.info
Tue Feb 11 23:10:57 CET 2014


On Tue, Feb 11, 2014 at 02:21:30PM -0500, Geoffrey Spear wrote:
> On Tue, Feb 11, 2014 at 1:38 PM, Liam Marsh <liam.marsh.home at gmail.com> wrote:
> > Hello,
> > do you think it is possible to operate with bytes like with ints?
> > useing the fact than
> > ord('A')==65 ;
> > will it possible to use b'A' like 65 or 00100001, in binary, for example for
> > encryption of files, etc...
> > thank you.
> 
> What idea are you proposing here? You can already call ord() on the
> single byte or iterate a bytes object to get integer values. Being
> able to treat b"A" itself as identical to the int 65, if that's what
> you're suggesting, doesn't really make sense.

It makes sense, that it to say the idea isn't entirely incoherent. It 
just is a bad idea.

That is to say, for this idea to work, we'd want things like these to be 
true:

b'A' == 65
which implies that int(b'A') == 65
b'A' < 100
b'CAT' == 4407636


Seems okay so far, but now we get into behaviour that contradicts 
existing behaviour:

b'<'*2 == b'x'  # instead of b'<<'
b'A' + b'B' == b'\x83'  # instead of b'AB'


And things which simply look wrong and bizarre:

int(b'2') == 50  # instead of 2
b'B' - b'A' == b'\x01'
b'A' - b'B' == -1
b'B'//2 == b'!'
b'B'/2 == 33.0


It would also imply some pretty strange things about ints: since b'CAT' 
equals 4407636, and b'CAT'[2] equals 84, does that mean we want to be 
able to say 4407636[2] and get 84?


-- 
Steven


More information about the Python-ideas mailing list