-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByte me!
More file actions
27 lines (14 loc) · 681 Bytes
/
Byte me!
File metadata and controls
27 lines (14 loc) · 681 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
If I were to ask you the size of "hello", what would you say?
Wait, let me rephrase the question:
If I were to ask you the total byte size of "hello", how many bytes do you think it takes up?
I'll give you a hint: it's not 5.
len("hello") --> 5
byte size --> 54
54? What?!
Here's why: every string has to have an encoding (e.g ASCII),the info that makes it an object, as well as all of it's other properites... it would have to take up a bit more than 5 bytes, wouldn't it?
This might be useful for sending something over a network, where you need to represent the memory size the item takes up.
*/
import sys
def total_bytes(obj):
return sys.getsizeof(obj)