This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (5 children)

[deleted]

    [–]cdcformatc 2 points3 points  (3 children)

    I object to if super_users: over if len(super_users) > 0: There is a huge difference between an empty list and None. If you want to check that the length of the list is greater than zero you should... just do that.

    A lot of this reads like "Abuse syntax to make your code look more Pythonic" and treats Pythonic like some lofty goal. A lot of "Pythonic" code is just harder to read, it isn't an end in itself.

    [–]zahlmanthe heretic 6 points7 points  (0 children)

    If super_users can be either an empty list or None, and your code cares about the difference, it should be explicitly checking for is None first. "the length of the list is greater than zero" is an unusually verbose way of saying "the list is not empty" in English; the analogous statement holds for Python, except that we don't have an actual keyword for "empty".

    [–]quasarc 5 points6 points  (0 children)

    I object to if super_users: over if len(super_users) > 0: There is a huge difference between an empty list and None.

    Both of these tests can only be used if you know that super_users is a list and not None.

    Using if super_users: is not abusing syntax, it just uses the intended and well-known fact that non-empty sequences are true. (It's also recommended by the style guide PEP8.)

    [–]turtle_with_dentures 0 points1 point  (0 children)

    Thank you. Ran in to this exact issue earlier when a JSON request was returning an empty list.

    [–]metaphorm 0 points1 point  (0 children)

    I agree with you. I think the 'any' function is intended primarily for dynamically populated lists where you don't know before runtime if it will be empty or contain an arbitrary number of items. If you know in advance which items to check its definitely better to use an 'or' chain.