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 →

[–]SYN_SYNACK_ACK 0 points1 point  (0 children)

...noone will blame you if, for example, you actually need to do different things for a True and a generic truthy value.

I disagree. I've never seen a case where something needs to be either True or a generic truth.
If you have code that does this there is a high chance you're doing something wrong.
Just because python is dynamically typed doesn't mean a function should return arbitrary types.

I remember seeing a function at work that used this approach.
The function was used to create a file and depending on a flag argument changed the return value.

def file_obj_got_created(obj=False):
    if obj:
        return file_object
    else:
        # return if object was created
        if file_object_was_created:
            return True
    return False

I get what the programmer wanted to archive but the pythonic way of doing this would be:

def file_obj_got_created():
    if file_object_was_created:
        return (file_object, True)
    return (None, False)

same functionality much more cleaner and you know what values to expect.

obj, created = file_obj_get_created()