By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,185 Members | 1,533 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,185 IT Pros & Developers. It's quick & easy.

Functions that raise exceptions.

P: n/a
Does anyone know how I would go about conditionally raising an
exception in a decorator (or any returned function for that matter)?
For example:

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

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

The intent of the above code is that an exception should be raised if
some_method is ever called. It seems, however, since the decorator
function is executed on import, the raise statement is executed, and I
the exception gets thrown whenever the module is imported, rather than
when the method is called. Does anyone have a clue how I might go
about doing this?

Thank you in advance,

Alex.
Jun 27 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
whoops! The code should be:

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

class some_class(object):
* * @decorator('meaningless string')
* * def some_method(self):
* * * * print "An exception should be raised when I'm called, but not
when I'm defined"
Jun 27 '08 #2

P: n/a
Alex G <al**************@gmail.comwrote:
Does anyone know how I would go about conditionally raising an
exception in a decorator (or any returned function for that matter)?
For example:

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

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

The intent of the above code is that an exception should be raised if
some_method is ever called. It seems, however, since the decorator
function is executed on import, the raise statement is executed, and I
the exception gets thrown whenever the module is imported, rather than
when the method is called. Does anyone have a clue how I might go
about doing this?
Well, the simplest way would be to correct the syntax error in your class
definition and to try calling the decorator you defined instead of calling
the undefined 'raise_exception'. Fix both of those and the code you posted
works 'as is'.
>>def decorator(arg):
def raise_exception(fn):
raise Exception
return raise_exception
>>class some_class(object):
@decorator
def some_method(self):
print "An exception should be raised when I'm called, but not when
I'm defined"

>>some_class().some_method()
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
some_class().some_method()
File "<pyshell#11>", line 3, in raise_exception
raise Exception
Exception
>>>
Jun 27 '08 #3

P: n/a
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...
Jun 27 '08 #4

P: n/a
Alex G <al**************@gmail.comwrote:
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.
Well, yes it would. That's because what you defined a decorator. i.e. a
function which wraps the method in another function. A decorator is called
with a single argument: the function/method to be wrapped.

What you have done in the code above is to call something named 'decorator'
which isn't a decorator at all, it's a function that you expect to return a
decorator (a decorator factory if you wish). If you make it return a
decorator then it will all work fine:
>>def decoratorfactory(s):
def decorator(arg):
def raise_exception(fn):
raise Exception
return raise_exception
return decorator
>>class some_class(object):
@decoratorfactory('somestring')
def some_method(self):
print "An exception should be raised when I'm called, but not when
I'm defined"

>>some_class().some_method()
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
some_class().some_method()
File "<pyshell#21>", line 4, in raise_exception
raise Exception
Exception
>>>
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.