Quick Reference from module doc strings.
Scott David Daniels
Scott.Daniels at Acm.Org
Sun May 15 23:31:01 EDT 2005
Ron Adam wrote:
> John Machin wrote:
>
>> Ron Adam wrote:
>>
>>> Does anyone have suggestions on how to improve this further?
>>
>> Not functionally (from me, yet). However if you can bear a stylistic
>> comment, do read on :-)
>>
>>> elif (isinstance(object,str)
>>> or isinstance(object,int)
>>> or isinstance(object,bool)
>>> or isinstance(object,tuple)
>>> or isinstance(object,list)
>>> or isinstance(object,dict)):
>>
>> Since Python version 2.2, the 2nd arg of isinstance may be a tuple. You
>> could define this up front, with a meaningful name:
>>
>> TYPES_WHICH_whatever = (str, int, bool, etc etc)
>
> Actually I'm begging for comments, it's the beginning of a project not
> the end. So thanks! ;-)
>
> I changed it to:
>
> if type(object)==str:
> ....
> elif type(object) in (str,int,bool,tuple,list,dict):
> ....
Althought object is a horrible name for your own value (there is a builtin
object which you use for defining new-style classes), you probably want:
if isinstance(object, (str,int,bool,tuple,list,dict)):
...
or (as John Machin was trying to suggest):
if isinstance(object, TYPES_WHICH_whatever):
...
This allows you to use instances of those builtin types and any
user-defined subtypes.
> Thanks, I don't need the isinstance(), type works here just as well.
But the isinstance version is better than the type(...) in ... version.
--Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list