Function Verification

Ws nothingcanfulfill at gmail.com
Tue Jun 6 21:44:37 EDT 2006


Ah, damn. That would've been soo much simpler. =S

Thanks for the advice man.

-Wes

Ben Cartwright wrote:
> Ws wrote:
> > I'm trying to write up a module that *safely* sets sys.stderr and
> > sys.stdout, and am currently having troubles with the function
> > verification. I need to assure that the function can indeed be called
> > as the Python manual specifies that sys.stdout and sys.stderr should be
> > defined (standard file-like objects, only requiring a function named
> > "write").
> <snip>
> > My problem is in verifying the class we're trying to redirect output
> > to.
> > This is what I have so far:
> > def _VerifyOutputStream(fh):
> >     if 'write' not in dir(fh):
> >         raise AttributeError, "The Output Stream should have a write
> > method."
> >     if not callable(fh.write):
> >         raise TypeError, "The Output Stream's write method is not
> > callable."
> <snip>
> > In the above _VerifyOutputStream function, how would I verify that the
> > fh.write method requires only one argument, as the built-in file
> > objects do?
>
> Why not just call the function with an empty string?
>
> def _VerifyOutputStream(fh):
>     fh.write('')
>
> Note that you don't need to manually check for AttributeError or
> TypeError.  Python will do that for you.  It's generally better to act
> first and ask forgiveness later.
> 
> --Ben




More information about the Python-list mailing list