<div dir="ltr">I fail to see why you can't have both args and kwargs...<div><br>class BaseException:<br>    @classmethod<br>    def format(cls, fmt, *args, **kwargs):<br>        return cls(fmt.format(*args, **kwargs), *args, **kwargs)<br>
<br></div><div>And concerning Gerog's actual question, I think this is a not uncommon pattern, but</div><div>    raise Exception.format(...)</div><div>doesn't sound good. It's not really as readable as</div><div>
    raise Exception(fmt.format(ext), ext)</div><div><br></div><div>ilya suggested an almost ok direction but he doesn't define formats at runtime. So maybe instead we can make a factory for formatted exceptions.</div>
<div><br></div><div>def formatted_exception(exception):</div><div>    def format_and_return(fmt, *args, **kwargs):</div><div>        return exception(fmt.format(*args, **kwargs), *args, **kwargs)</div><div>    return format_and_return</div>
<div><br></div><div><div>@formatted_exception</div><div>class NotRegisteredError(LookupError): pass</div><div><br></div></div><div>then the usage becomes:</div><div>raise NotRegisteredError("Extension {0} not registered.", ext)</div>
<br><div class="gmail_quote">On Fri, Sep 18, 2009 at 10:54 PM, ilya <span dir="ltr"><<a href="mailto:ilya.nikokoshev@gmail.com">ilya.nikokoshev@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
How about simplifying creation of a subclass for this pattern::<br>
<br>
class NotRegisteredError(LookupError):<br>
    template = 'Extension {1} not registered'<br>
<br>
...<br>
raise NotRegisteredError(ext)<br>
<br>
<br>
This is instead of::<br>
<br>
class NotRegisteredError(LookupError):<br>
    def __init__(self, *args):<br>
        super().__init__('Extension {1} not registered'.format(self,<br>
*args), *args)<br>
<br>
<br>
Advantages to having a separate subclass:<br>
(1) it can be reused<br>
(2) it can be caught separately from LookupError<br>
(3) you can list possible exceptions in the beginning of module<br>
(4) you can search for functions that raise a specific exception<br>
<br>
What do you think?<br>
<font color="#888888"><br>
Ilya.<br>
</font><div><div></div><div class="h5"><br>
On Fri, Sep 18, 2009 at 6:07 PM, Georg Brandl <<a href="mailto:g.brandl@gmx.net">g.brandl@gmx.net</a>> wrote:<br>
> To make Exceptions where some object(s) are involved more useful, it is<br>
> often necessary to put the objects on the exception *in addition to*<br>
> formatting them into a string representation for the message.<br>
><br>
> This little classmethod would make that easier::<br>
><br>
>  class BaseException:<br>
>    @classmethod<br>
>    def format(cls, fmt, *args):<br>
>      return cls(fmt.format(*args), *args)<br>
><br>
> Example usage::<br>
><br>
>  ext = 'foo'<br>
>  raise LookupError.format('Extension {0} not registered', ext)<br>
><br>
> 'foo' could then be accessed as ``exc.args[1]``.<br>
><br>
><br>
> A similar, but also very useful implementation would be ::<br>
><br>
>  def format(cls, fmt, **kwds):<br>
>    exc = cls(fmt.format(**kwds))<br>
>    exc.__dict__.update(kwds)<br>
>    return exc<br>
><br>
> with example usage being::<br>
><br>
>  raise LookupError.format('Extension {ext} not registered', ext='foo')<br>
><br>
> and 'foo' being accessible as ``exc.ext``.<br>
><br>
> I realize this is probably too obscure for Python core, but I wanted to<br>
> show it to you anyway, maybe it'll be found useful.<br>
><br>
> Georg<br>
><br>
> --<br>
> Thus spake the Lord: Thou shalt indent with four spaces. No more, no less.<br>
> Four shall be the number of spaces thou shalt indent, and the number of thy<br>
> indenting shall be four. Eight shalt thou not indent, nor either indent thou<br>
> two, excepting that thou then proceed to four. Tabs are right out.<br>
><br>
> _______________________________________________<br>
> Python-ideas mailing list<br>
> <a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
> <a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/mailman/listinfo/python-ideas</a><br>
><br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/mailman/listinfo/python-ideas</a><br>
</div></div></blockquote></div><br></div>