Encoding questions (continuation)

Fábio Santos fabiosantosart at gmail.com
Mon Jun 10 12:17:12 EDT 2013


On 10 Jun 2013 15:04, "Νικόλαος Κούρας" <nikos.gr33k at gmail.com> wrote:
>
> Τη Δευτέρα, 10 Ιουνίου 2013 2:41:07 μ.μ. UTC+3, ο χρήστης Steven D'Aprano
έγραψε:
> > On Mon, 10 Jun 2013 14:13:00 +0300, Νικόλαος Κούρας wrote:
> >
> >
> >
> > > Τη Δευτέρα, 10 Ιουνίου 2013 1:42:25 μ.μ. UTC+3, ο χρήστης Andreas
> >
> > > Perstinger έγραψε:
> >
> > >
> >
> > >  >  >>> s = b'\xce\xb1'
> >
> > >  >
> >
> > >  >  >>> s[0]
> >
> > >  >
> >
> > >  > 206
> >
> > >
> >
> > > 's' is a byte object, how can you treat it as a string asking to
present
> >
> > > you its first character?
> >
> >
> >
> > That is not treating it as a string, and it does not present the first
> >
> > character. It presents the first byte, which is a number between 0 and
> >
> > 255, not a character.
> >
> >
> >
> > py> alist = [0xce, 0xb1]
> >
> > py> alist[0]
> >
> > 206
>
>
> To my mind alist[0] should yield '0xce'

This happens because 0xce is an integer object too. Integer objects have no
"memory" of what format they were declared on. Oxce just happens to be 206
in hexadecimal. Ob10 would also result in the integer 2.

> > Is that treating alist as a string? No, of course not. Strings are not
> >
> > the only object that have indexing object[position].
>
> Yes actually it does.
>
> s string is a series of characters.
>
> a list is a series of objects, which can be chars, strings, integers,
other data structures.
>
> So doing a_list[0] is similar of doing a_string[00
>
>
> > >  > A byte object is a sequence of bytes (= integer values) and support
> > > indexing
>
>
> Isn't a byte a series of zeros and ones, like 01010101 ?
> So why you say bytes are integers since its numbers into a binary system?
> perhsp you mean a represantaion of a bye to a decimal value?

A byte is a number, stored in (usually) 8 binary digits. By addressing a
bytes object by its index, you get an integer of that byte. That's just how
python deals with it.

You should heed Steven's advice and read up on basic computing. You seem to
be lacking a lot of basic concepts.

Cheers.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130610/b005e1e3/attachment.html>


More information about the Python-list mailing list