[Python] hasattr() vs try-except block to deal with non-existent attributes

Discussão em 'Python' iniciado por Stack, Julho 20, 2025.

  1. Stack

    Stack Membro Participativo

    if hasattr(obj, 'attribute'):
    # do somthing


    vs

    try:
    # access obj.attribute
    except AttributeError, e:
    # deal with AttributeError


    Which should be preferred and why?

    Continue reading...

Compartilhe esta Página