[Python] Python generator objects: __sizeof__()

Discussão em 'Python' iniciado por Stack, Abril 4, 2025.

  1. Stack

    Stack Membro Participativo

    This may be a stupid question but I will ask it anyway. I have a generator object:

    >>> def gen():
    ... for i in range(10):
    ... yield i
    ...
    >>> obj=gen()


    I can measure it's size:

    >>> obj.__sizeof__()
    24


    It is said that generators get consumed:

    >>> for i in obj:
    ... print i
    ...
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    >>> obj.__sizeof__()
    24


    ...but obj.__sizeof__() remains the same.

    With strings it works as I expected:

    >>> 'longstring'.__sizeof__()
    34
    >>> 'str'.__sizeof__()
    27


    I would be thankful if someone could enlighten me.

    Continue reading...

Compartilhe esta Página