[Python] Using a class property as an iterable produces a reassign warning

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

  1. Stack

    Stack Membro Participativo

    I need to use an iterable and a loop variable as a class property. But the flake8 checker produces B2020 warning:

    easy.py:11:13: B020 Found for loop that reassigns the iterable it is iterating with each iterable value.


    If I use a variable for iterable there is OK.

    What is wrong?

    The warning example:

    #!/usr/bin/env python3
    """Example of B020 error."""


    class My_Template:
    """My example."""

    def __init__(self, *template):
    """Obviously init."""
    self.all_templates = (1, 2, 3)
    for self.tpl in self.all_templates:
    print(self.tpl)



    The flake8 complains about loop variable:

    easy.py:11:13: B020 Found for loop that reassigns the iterable it is iterating with each iterable value.


    The OK example:

    #!/usr/bin/env python3
    """Example of B020 error."""


    class My_Template:
    """My example."""

    def __init__(self, *template):
    """Obviously init."""
    all_templates = (1, 2, 3)
    for self.tpl in all_templates:
    print(self.tpl)

    Continue reading...

Compartilhe esta Página