Functions that raise exceptions.

Alex G alexander.girman at gmail.com
Wed Jun 25 16:44:34 EDT 2008


I'm sorry about the typos, but that doesn't seem to be what the issue
is (I typed it into the textbox rather carelessly, I apologize :-( ).
It seems to be an issue with passing the decorator an argument:

Given:

def decorator(arg):
    def raise_exception(fn):
        raise Exception
    return raise_exception

If you pass the decorator an argument, it doesn't work as expected
(but if you neglect the argument, it works, even though the decorator
_expects_ an argument.

That is,

class classA(object):
    @decorator('argument')
    def some_method(self):
        print "An exception should be raised when I'm called, but not
when I'm defined"

Will result in an exception on definition.

class classB(object):
    @decorator
    def some_method(self):
        print "An exception should be raised when I'm called, but not
when I'm defined"

Gets defined, and executing

b = classB()
b.some_method()

>>> b = classB()
>>> b.some_method()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in raise_exception
Exception

works as expected, even though decorator is expecting an argument...



More information about the Python-list mailing list