[Python-ideas] AtributeError inside __get__
Zahari
zaharid at gmail.com
Tue Dec 27 05:11:24 EST 2016
> So here's a two-part proposal that would solve Zaheri's problem:
>
> 1) Enhance AttributeError to include arguments for the parts in
> quotes, for i18n independence.
> 2) Provide, in the docs, a hasattr replacement that checks the exception's
> args.
>
> The new hasattr would look like this:
>
> def hasattr(obj, name):
> try:
> getattr(obj, name)
> return True
> except AttributeError as e:
> if e.args[1] == obj.__class__.__name__ and e.args[2] == name:
> return False
> raise
>
> Since it's just a recipe in the docs, you could also have a version
> that works on current Pythons, but it'd need to do string manipulation
> to compare - something like:
>
> def hasattr(obj, name):
> try:
> getattr(obj, name)
> return True
> except AttributeError as e:
> if e.args[0] == "%r object has no attribute %r" % (
> obj.__class__.__name__, name):
> return False
> raise
>
> I can't guarantee that this doesn't get some edge cases wrong, eg if
> you have weird characters in your name. But it'll deal with the normal
> cases, and it doesn't need any language changes - just paste that at
> the top of your file.
>
> Zaheri, would this solve your problem?
>
This looks like a good idea. Note that there is also getattr(X(), 'y',
'default') that would have to behave like this.
Cheers,
Zahari
>
> ChrisA
> _______________________________________________
> Python-ideas mailing list
> Python... at python.org <javascript:>
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161227/94a5e5f5/attachment.html>
More information about the Python-ideas
mailing list