[Python] Test for multiple variables in locals().keys() fails [duplicate]

Discussão em 'Python' iniciado por Stack, Maio 26, 2025.

  1. Stack

    Stack Membro Participativo

    I am testing the existence of local variables LatLim and LongLim at the Spyder console:

    >>> LatLim = (36.033333,36.2333)
    >>> LongLim = (-5.5333,-5.25)
    >>> locals().keys()
    dict_keys(['__name__', '__builtin__', ... 'LatLim', 'LongLim', ... ])


    Each variable tests fine by itself:

    >>> 'LatLim' in locals().keys()
    True

    >>> 'LongLim' in locals().keys()
    True


    The OR'ing of the test should be True but is False:

    >>> any( key in locals().keys()
    for key in ('LatLim','LongLim') )
    False


    If I create a dictionary of local keys, the test succeeds:

    >>> LocalsKeys = locals().keys()
    >>> any( key in LocalsKeys for key in ('LatLim','LongLim') )
    True


    Why does the OR'ing fail when checking locals().keys() directly?

    Continue reading...

Compartilhe esta Página