3 ms·
Looks like a nice little module. However, just some comments on the (lack of) Pythonicity in this function: def is_string(obj): """Tests if an obj
by benhoyt 16y ago
Looks like a nice little module.
However, just some comments on the (lack of) Pythonicity in this function:
def is_string(obj):
"""Tests if an object is a string"""
return True if type(obj).__name__ == 'str' else False
First, you don't need the "True if ... else False", because == is already a boolean operator which returns True or False.
Also, you don't need to look at the type's __name__. You can just say:
type(obj) == str
But Python already has an isinstance() function which will also work properly with subclasses of str, so suddenly you don't need the is_string() helper function anymore, and it becomes:
isinstance(obj, str)
(Though you should probably use "basestring" in Python 2.x so it works with Unicode keys too.)
- kennethreitz 16y agoThanks for the input. The whole thing is def a work in progress. I put it together in about 4 hours, and that function in particular was some copy pasta from a project a few years ago. No worries, updates to come :)