[Chicago] Prevent access to method as attribute

Brian Ray brianhray at gmail.com
Wed Oct 17 14:39:07 CEST 2012


This is very cool and clean too. Dare I say Pythonic? Good job
everyone and thanks!

On Wed, Oct 17, 2012 at 6:41 AM, Ken Schutte <kenschutte at gmail.com> wrote:
> Along similar lines, you could try this:
>
>
> class notAttribute(object):
>     def __init__(self, func):
>         self.func = func
>
>     def __get__(self, obj, type = None):
>         return self.__class__(self.func.__get__(obj, type))
>
>     def __nonzero__(self):
>         raise Exception("Don't check me as an attribute!  Call me first!")
>
>     def __call__(self,*a,**kw):
>         return self.func(*a,**kw)
>
>
> Usage:
>
> class A(object):
>
>     @notAttribute
>     def isValid(self):
>         return False
>
>
> a = A()
>
> try:
>     if a.isValid:
>         print "(1) valid"
>     else:
>         print "(1) not valid"
> except Exception, e:
>     print "ERROR:", e
>
> if a.isValid():
>     print "(2) valid"
> else:
>     print "(2) not valid"
>

-- 
Brian Ray
@brianray


More information about the Chicago mailing list