Since this seems like it would mostly be useful for beginners, I'd focus on the most common case -- forgetting to call an instance method in a boolean context (like an "if" statement).<br><br><br><br>from types import MethodType<br>

<br>class UnBoolableCallable(object):<br>    def __init__(self, call):<br>        self.call = call<br>    def __call__(self, *args, **kwargs):<br>        return self.call(*args, **kwargs)<br>    def __len__(self): # why doesn't __bool__ work? check on this.<br>

        raise Exception("You forgot to call me!")<br><br># we could do this in __new__ on the class<br># since we're only customizing instance creation, not class creation<br>class AngryObject(type):<br>    def __call__(cls, *args, **kwargs):<br>

        # create the instance<br>        instance = super(AngryObject, cls).__call__(*args, **kwargs)<br>        # for each attribute in the class dict<br>        for attr in vars(cls):<br>            # get the object from the instance (so as to bind methods)<br>

            value = getattr(instance, attr)<br>            # if it's an instance method<br>            if isinstance(value, MethodType):<br>                # wrap it in an un-boolable type<br>                setattr(instance, attr, UnBoolableCallable(value))<br>

        return instance<br><br>class A(object):<br>    __metaclass__ = AngryObject<br>    def isValid(self):<br>        return False<br><br># first two work, third raises<br>for result in A().isValid(), A.isValid, A().isValid:<br>

    print result, type(result), bool(result)<br><br><br><br><br>P.S. If you really want this behavior use Ruby.<br><br><br><br><div class="gmail_quote">On Tue, Oct 16, 2012 at 11:58 AM, Brian Ray <span dir="ltr"><<a href="mailto:brianhray@gmail.com" target="_blank">brianhray@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Question came up from a colleague:<br>
<br>
I wonder, if there is a simple way in Python to add a hook to class that<br>
makes sure methods don't not get called as attributes.<br>
<br>
class A:<br>
    def isValid(self):<br>
           return False<br>
<br>
<br>
if A().isValid:<br>
    print 'Always True'<br>
<br>
<br>
<br>
I had some wacky and wild thoughts about subclassing something to<br>
check each and every call and using inspect module to see how it was<br>
called. Generally, I know it is the callers responsibility to know<br>
what they are doing. We are all responsible adults here, correct?<br>
<span class="HOEnZb"><font color="#888888"><br>
<br>
<br>
--<br>
Brian Ray<br>
@brianray<br>
<a href="tel:%28773%29%20669-7717" value="+17736697717">(773) 669-7717</a><br>
_______________________________________________<br>
Chicago mailing list<br>
<a href="mailto:Chicago@python.org">Chicago@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/chicago" target="_blank">http://mail.python.org/mailman/listinfo/chicago</a><br>
</font></span></blockquote></div><br>