Mostly Linux & Python syntax notes and hyperlinks.

Sunday, July 27, 2014

assert isinstance() not for file stream parameters?

I'm writing methods in PyCharm, and I'd like to follow its hints for the way I should be structuring my code. Here I'm passing in a parameter of type file stream, could be sys.out or a file descriptor. It prompted me to include an "assert isinstance()" for the input parameter. 

    def set_stream(self,fdout):
        """
        :param fdout: 
        """ 
        assert isinstance(fdout,...) 

I was looking up what type to call it for the purposes of isinstance() and I came across  http://dobesland.wordpress.com/2007/10/07/python-isinstance-considered-useful/:
The classic example of this is python’s famous “file-like objects”, which typically implement read and/or write in the same way and are accepted by various python functions.  I believe the DB API is another well-used example of this.  In both cases, there is no common base-class, so it’s impossible to use isinstance() to check whether a particular object is, in fact, file-like or a database object.
I guess I'll leave that assert() out. The code will break reliably enough when someone tries to write to an fdout that isn't the right sort of object.

No comments:

Post a Comment