-2

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:

enter image description here

Debug Shell in Pycharm:

enter image description here

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?

2
  • 1
    Why would you even expect those numbers to be equal? Commented Jul 28, 2022 at 11:22
  • Please check function calculation first of all which is: sys.getsizeof(data) - sys.getsizeof(data.__class__()) why wouldn't it be same? Commented Jul 28, 2022 at 14:12

1 Answer 1

0

It is unreasonable to expect anything like that from an implementation you don't control.

It is ok for the python implementation to create two separate objects for the first and second occurrence of the "question" string literal. Possibly some kind of string implementation for the first one, and a string-pooling object for the second?

Refer to this answer for details: https://stackoverflow.com/a/30260285/6610; it happens to talk about the str case, C source code included.

Sign up to request clarification or add additional context in comments.

1 Comment

Updated question to highlight issue since it's not well understood. Would you be able to address why would debug v/s normal run reports 2 bytes difference for same command: sys.getsizeof('question'.__class__()) ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.