I have already referred that sys.getsizeof returns size of string including it's own class size, so eventually If I am supposed to get the size of class itself I can actually get absolute size of any datatype for my application.
Well, also note that, as per Python3 implementation, str class size should be 49 bytes and followed by addition of numbers of characters. Hence as per my operation, sys.getsizeof('question'.__class__()) should return me value 49 which is happening in case of normal run of pytest, however when switched to debug mode, I observed it is producing value 51 instead.
So, I hope folks should consider to real problem of inconsistency reported in size of same string between normal run v/s debug run.
Check this snapshot as well:
Normal run:
Debug Shell in Pycharm:
I have established below simple test snippet out of the project:
import sys
import unittest
def sizeof(data):
if isinstance(data, int):
bit_length = len(bin(data)[2:])
return bit_length // 8 + (1 if bit_length % 8 else 0)
return sys.getsizeof(data) - sys.getsizeof(data.__class__())
class UnitTestHelper(unittest.TestCase):
def test_sizeof(self):
self.assertEqual(sizeof(1), 1, "size for - {} must be 1 byte!".format(1))
self.assertEqual(sizeof((2 ** (8 * 40)) - 1), 40, "size for - {} must be 40 bytes!".format((2 ** (8 * 40)) - 1))
for i in range(10, 200):
val = 2**(8*i) - 1
self.assertEqual(sizeof(val), i, "size for - {} must be {} bytes!!".format(val, i))
self.assertEqual(sizeof("question"), len("question"), "size mismatch!!!")
If I run above program as Run mechanism in Pycharm (with Pytest) I get the result as:
Launching pytest with arguments MyTest.py::UnitTestHelper::test_sizeof --no-header --no-summary -q in C:\Test
============================= test session starts =============================
collecting ... collected 1 item
MyTest.py::UnitTestHelper::test_sizeof PASSED [100%]
======================== 1 passed, 1 warning in 0.02s =========================
If I run it with debug with Pytest I get result:
Launching pytest with arguments MyTest.py::UnitTestHelper::test_sizeof --no-header --no-summary -q in C:\Test
============================= test session starts =============================
collecting ... collected 1 item
MyTest.py::UnitTestHelper::test_sizeof FAILED [100%]
size mismatch!!!
8 != 6
Expected :6
Actual :8
<Click to see difference>
self = <MyTest.UnitTestHelper testMethod=test_sizeof>
def test_sizeof(self):
self.assertEqual(sizeof(1), 1, "size for - {} must be 1 byte!".format(1))
self.assertEqual(sizeof((2 ** (8 * 40)) - 1), 40, "size for - {} must be 40 bytes!".format((2 ** (8 * 40)) - 1))
for i in range(10, 200):
val = 2**(8*i) - 1
self.assertEqual(sizeof(val), i, "size for - {} must be {} bytes!!".format(val, i))
> self.assertEqual(sizeof("question"), len("question"), "size mismatch!!!")
MyTest.py:20: AssertionError
======================== 1 failed, 1 warning in 0.64s =========================
Same failure result I noticed with JetBrains Teamcity Pytest configuration as well.
What could be the possible reason for such inconsistency of class variables?


sys.getsizeof(data) - sys.getsizeof(data.__class__())why wouldn't it be same?