Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python __sizeof__() Method

#1
Python __sizeof__() Method

Syntax


object.__sizeof__(self, other)

The Python __sizeof__() method returns the size of the object in bytes. The sys.getsizeof() method internally call’s __sizeof__() and adds some additional byte overhead, e.g., for garbage collection.

Basic Example


The following code snippet creates a list object x with three integers and measures its size in bytes (104) using the x.__sizeof__() method call.

It then measures the size with overhead using sys.getsizeof(x).

>>> import sys
>>> x = [1, 2, 3]
>>> x.__sizeof__()
104
>>> sys.getsizeof(x)
120

Let’s make the list much larger to measure if it has any impact on the size in bytes.

>>> import sys
>>> x = list(range(10000))
>>> x.__sizeof__()
80040
>>> sys.getsizeof(x)
80056

This list is built using the range() function. It has 10000 integers, each needing 8 bytes with a constant overhead of 40 bytes for the list structure, the x.__sizeof__() method call results in 10000 * 8 + 40 = 80040 bytes.

You can see that in both cases, the sys.getsizeof(x) has 16 more bytes than x.__sizeof__() which seems to be the overhead on my particular machine for this particular example.

Overriding __sizeof__()


You can override the __sizeof__() method for your own custom data type by defining the __sizeof__() method with one argument self (that is passed automatically by Python).

import sys class Data: def __sizeof__(self): return 42 x = Data()
print(x.__sizeof__())

The __sizeof__() method returns the integer 42 that was returned by us—not the number of bytes.

Let’s call the sys.getsizeof() method to see if the difference is 16 bytes for the GC overhead!

import sys class Data: def __sizeof__(self): return 42 x = Data() print(x.__sizeof__())
# 42 print(sys.getsizeof(x))
# 58

References:

Where to Go From Here?


Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!



https://www.sickgaming.net/blog/2021/12/...__-method/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

[-]
Discord

[-]
Active Threads
(Indie Deal) Handy Games, Offworld, Citi...
Last Post: xSicKxBot
Yesterday 10:43 AM
» Replies: 0
» Views: 37
News - Pokemon-With-Guns Game Palworld C...
Last Post: xSicKxBot
Yesterday 10:42 AM
» Replies: 0
» Views: 10
(Xbox 360) Mod Forza Horizon 2 with USB
Last Post: tekashisama
Yesterday 02:45 AM
» Replies: 11
» Views: 10535
[Tut] How to Create a NumPy Array and Fi...
Last Post: xSicKxBot
Yesterday 02:45 AM
» Replies: 0
» Views: 31
(Indie Deal) War Giveaways, Akupara, Hum...
Last Post: xSicKxBot
Yesterday 02:45 AM
» Replies: 0
» Views: 13
News - Death’s Gambit: Afterlife Physica...
Last Post: xSicKxBot
Yesterday 02:44 AM
» Replies: 0
» Views: 15
News - Xbox Game Pass Adding Another Day...
Last Post: xSicKxBot
Yesterday 02:44 AM
» Replies: 0
» Views: 18
[Tut] How To Apply A Function To Each El...
Last Post: xSicKxBot
01-26-2022 04:46 PM
» Replies: 0
» Views: 27
(Indie Deal) Exotic Riddles Bundle, Dyin...
Last Post: xSicKxBot
01-26-2022 04:45 PM
» Replies: 0
» Views: 16
Mobile - Pokémon Sword and Shield myster...
Last Post: xSicKxBot
01-26-2022 04:45 PM
» Replies: 0
» Views: 13

[-]
Twitter



Discord Server © SickGaming.net 2012-2021