[Python] Why does sys.getsizeof() report the same size for two instances even though one has...

Discussão em 'Python' iniciado por Stack, Março 3, 2025.

  1. Stack

    Stack Membro Participativo

    import sys

    class MyObject:
    def __init__(self,name,salary):
    self.name = name
    self.salary = salary

    obj1 = MyObject("Tushar", 50)
    obj2 = MyObject("Tushar", 50)
    obj2.age = 20

    print(sys.getsizeof(obj1)) # 56
    print(sys.getsizeof(obj2)) # 56


    Why is obj2 only 56 bytes when it has an extra age attribute?

    Continue reading...

Compartilhe esta Página