[BioPython] suggestions for Bio.PDB

Andrew Dalke dalke at dalkescientific.com
Sun Apr 17 13:50:30 EDT 2005


Hi Thomas,

> I used:
>
> def get_structure(self, id, file):
>     ...
>     if type(file)==types.StringType:
>             file=open(file)

You should use
   isinstance(file, basestring)

for two reasons.  As of a few Python releases ago, Python
allows unicode filenames but your check doesn't because

 >>> import types
 >>> type("") == types.StringType
True
 >>> type(u"") == types.StringType
False
 >>>

Also, some people do derive from string.  The most relevant
example is the 'path' module at
   http://www.jorendorff.com/articles/python/path/

which points out in the "Warts" section

      A function that checks the type of its parameters
      with code like if type(s) is type(''): ..., will
      fail to recognize a path object as a string. This
      may be considered a bug in the function, though.
      The test should be written
         if isinstance(s, (str, unicode)),
      or in Python 2.3,
         if isinstance(s, basestring),
      both of which allow for string subclasses.


					Andrew
					dalke at dalkescientific.com



More information about the BioPython mailing list