简体   繁体   中英

python classes inner logic

1 - I'm using some libraries for python and I'm curious about how they works internally for instance:

from urllib.request import urlopen
print(type(urlopen)) # <class 'function'>

2 - let say Html page contain this text:

This is line one
This is line two

returns for first line:

b'This is line one\n'

?? I know It's byte type But why we have actual words in it instead I expected I see some thing like this: 001001011010101010101010101010000101010 which every char translated into base binary of it's corresponding utf-8 code. I don't get what leading b is here what is this syntax?

3 - If I use decode().split() on this line I loose '\n' in returned list:

[This, is, line, one]

why this happen? Thank you for your great support for newbie developers.

You get byte code if you encode unicode with an encoding. (encode because it is readable for "your machine").

You get unicode, if you decode bytecode with an encoding. (because you may read it)

This means, the b'...' is in byte format (already encoded).

An example for an encoding is UTF-8

If you want binary digits (eg 0101010), use buildin bin() function. Eg:

bin(42)

results in:

'0b101010'

Look at the 0b prefix, it is different from b'...'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM