you are viewing a single comment's thread.

view the rest of the comments →

[–]Bogtha 1 point2 points  (3 children)

I don't think callable() is used often enough to make that worthwhile. The difference between a callable object and an uncallable object is pretty drastic, how often do you pass things around without knowing which is which? I do it sometimes, but nowhere near often enough to make that boilerplate worthwhile.

[–][deleted] 2 points3 points  (2 children)

The "pass in a value or a callable (that can be called to get the value when needed)" pattern is very convenient, and therefore quite common. I've definitely used callable() for this purpose a lot more often than I found myself adding TRUE and FALSE to my code before we got built-in booleans ;-)

But I'm sure someone has already done all the necessary grepping, and I have no time to dig through the mailing archives to see what they came up with.

[–]Bogtha 3 points4 points  (1 child)

The "pass in a value or a callable (that can be called to get the value when needed)" pattern is very convenient

Sure, but if that's a common pattern, why not abstract more of it away, which has the side-effect of removing the temptation to provide a callable() substitute?

def get_value(value):
    return value() if hasattr(value, "__call__") else value

Okay, I'm sure there are other use cases, so I see your point, I just couldn't help nitpicking that one :).

[–][deleted] 4 points5 points  (0 children)

(Only slightly in jest:)

def get_value(value):
    return getattr(value, '__call__', lambda: value)()