[Chicago] Prevent access to method as attribute

Brantley Harris deadwisdom at gmail.com
Thu Oct 18 14:11:22 CEST 2012


This seems like something that should be done at testing or some syntax
checking stage, like PyLint.  Sure you can solve it this way, it's in fact
rather interesting and clever, but it adds a whole lot of complexity for
little gain.

Always Be Testin'

On Wed, Oct 17, 2012 at 7:39 AM, Brian Ray <brianhray at gmail.com> wrote:

> 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
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20121018/cb153d8f/attachment.html>


More information about the Chicago mailing list