My Python annoyances

Chris Mellon arkanes at gmail.com
Fri May 4 10:30:06 EDT 2007


On 5/4/07, Ben Collver <collver at peak.org> wrote:
> Ben Collver wrote:
> > Chris Mellon wrote:
> >> Code like this is working directly against Python philosophy. You
> >> probably got told this on #python, too. There's hardly any
> >> circumstance where you should need to validate the exact class of an
> >> object, and as long as they have the same interface theres no harm
> >> whatsoever in tempfile changing it's return value between Python
> >> versions.
> >
> > I am unqualified to comment on the Python philosophy, but I would like
> > for my function to do some basic error checking on its arguments.
>
> By "basic error checking" I mean "verify that the file argument actually
> is a file-like object".  By same interface, do you mean that I should
> check for the methods that I depend on?  That sounds easy enough.
>

You should "check" for the methods by calling them. If the object
doesn't support the method in question, you will get a runtime
exception. Premature inspection of an object is rarely useful and
often outright harmful.



More information about the Python-list mailing list