Abuse of the object-nature of functions?

Ant antroy at gmail.com
Tue Jul 11 12:47:24 EDT 2006


Sybren wrote:
> Try to make a habit out of catching only the exceptions you know will
> be thrown.

Yes - you are right of course - this was just a minimal example of
course to illustrate the sort of thing I'm using function attriutes
for.

Fredrik Lundh wrote:
> Peter Otten wrote:
> > assertion = getattr(callable, "is_assertion", False)

I like this way of doing it, as it is possible I may have a function
that could feasibly be either an assertion or otherwise depending on
context.

> agreed (but note that the OP used a decorator to set the attribute, so you could
> consider it an implementation detail...)

The main reason for using a decorator was to avoid problems like the
following whilst providing an easy way of adding the attribute:

>>> def assn_or_other(data):
...     return False
...
>>> # I want x to be treated as normal.
... x = assn_or_other
>>>
>>> # I want y to be an assertion
... y = assn_or_other
>>> y.is_assertion = True
>>> x.is_assertion
True




More information about the Python-list mailing list