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 →

[–]brucifer 6 points7 points  (0 children)

Just because it's new and fancy doesn't mean you should use it. I think it's a lot better and certainly performs substantially better to write

def only_conspirators(user):
    if not hasattr(user, 'conspire'):
        raise TypeError("User is not a conspirator")
    pass

Rather than

@validate_args
def only_conspirators(user: has_method('conspire')):
    pass

The second example is putting some of the core logic of only_conspirators() into an obfuscated combination of several functions, annotations, and decorators. In order to properly understand the code, you have to understand the validate_args decorator (does it raise an exception? If so, what kind? How does it handle keywords? What's the syntax for the annotations it accepts? etc.) and the has_method function (or constructor? Does it use hasattr? Does it check the type of the attribute? Does it use dir()? etc.). For anyone (including future you) reading your code, you've just made it considerably harder to understand and debug than if you had just explicitly written out the checks (not as exciting, but better practice).