If a function (or other non-string object) is accidentally passed as an argument to os.path.join() the result is an AttributeError:<div><br><div><br></div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
In [3]: os.path.join(fn, "path")<br>---------------------------------------------------------------------------<br>AttributeError                            Traceback (most recent call last)<br>/home/tom/<ipython-input-3-44b097ceab04> in <module>()<br>
----> 1 os.path.join(fn, "path")<br>/usr/lib/python2.7/posixpath.pyc in join(a, *p)<br>     66         if b.startswith('/'):<br>     67             path = b<br>---> 68         elif path == '' or path.endswith('/'):<br>
     69             path +=  b<br>     70         else:<br>AttributeError: 'function' object has no attribute 'endswith'</blockquote></div></div><div><br></div><div><br></div><div>It's relatively easy to run into this if you mean to pass the return value of a function (fn()) as the argument but accidentally forget to append parens (()) to the callable, thus passing the function itself instead.</div>
<div><br></div><div>Would it not be more helpful to raise a TypeError("Argument must be a string") than the slightly more mysterious AttributeError?</div><div><br></div><div>It's not the most difficult error in the world to figure out, I admit, but a TypeError just seems like the more correct thing to do here.</div>
<div><br></div><div>Thanks,<br>Tom</div>