[Chicago] Prevent access to method as attribute

Alex Gaynor alex.gaynor at gmail.com
Tue Oct 16 19:09:53 CEST 2012


On Tue, Oct 16, 2012 at 9:58 AM, Brian Ray <brianhray at gmail.com> wrote:

> Question came up from a colleague:
>
> I wonder, if there is a simple way in Python to add a hook to class that
> makes sure methods don't not get called as attributes.
>
> class A:
>     def isValid(self):
>            return False
>
>
> if A().isValid:
>     print 'Always True'
>
>
>
> I had some wacky and wild thoughts about subclassing something to
> check each and every call and using inspect module to see how it was
> called. Generally, I know it is the callers responsibility to know
> what they are doing. We are all responsible adults here, correct?
>
>
>
> --
> Brian Ray
> @brianray
> (773) 669-7717
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago
>

Not a sane one know. The closest you could do would be to have a descriptor
that kept a flag on itself marking whether it was called, and then have an
__del__ that raised an error if it wasn't called.

Like I said, really not sane.

Alex

-- 
"I disapprove of what you say, but I will defend to the death your right to
say it." -- Evelyn Beatrice Hall (summarizing Voltaire)
"The people's good is the highest law." -- Cicero
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20121016/ad3a30dc/attachment.html>


More information about the Chicago mailing list