[Chicago] What to do about File Like Object methods I don't need...

Brantley Harris deadwisdom at gmail.com
Mon Dec 22 20:20:04 CET 2008


Ah good point.

But, I guess I think of it more as: use this method, and you will get
an unhandled exception, which means you're using it wrong, and more
over it means that the interface does not support the method you're
trying to invoke, rather than the ambiguous state bearing questions
like "Did I use the right method?" or even "Am I sure I'm thinking of
the right interface?"

True, I wouldn't write out the long try/except, but I also wouldn't
write hasattr.  I'd just call it: fb.isatty(), because when I write
the code, having a specific interface in mind, I would not check every
method before I call it.

But, of course, we are arguing inches here.

On Sun, Dec 21, 2008 at 3:05 PM, Ian Bicking <ianb at colorstudy.com> wrote:
> Brantley Harris wrote:
>>
>> It's what NotImplimentedError is for!
>
> Nah, lots of code does things like:
>
> if hasattr(fp, 'isatty'):
>    isatty = fp.isatty()
> else:
>    isatty = False
>
> But not much code does:
>
> try:
>    isatty = fp.isatty()
> except (NotImplementedError, AttributeError):
>    isatty = False
>
>
> Maybe the second form makes sense, but I haven't seen it much.  And really a
> longer form would be better (at which point it's clearly becoming annoying,
> so people don't do it):
>
> try:
>    meth = fp.isatty
> except AttributeError:
>    isatty = False
> else:
>    try:
>        isatty = meth()
>    except NotImplementedError:
>        isatty = False
>
>
> --
> Ian Bicking : ianb at colorstudy.com : http://blog.ianbicking.org
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago
>


More information about the Chicago mailing list