python - Why does type checking not work for what type returns? -
i tried test:
if type(model_lines) == 'str': turn list using split based on:
in [196]: type('a') out[196]: str however, x, string:
in [193]: if type(x) == 'str': print 'string' .....: in [195]: if type(x) == type('a'): print 'string' .....: string i curious why cannot use output check types, seems cleaner , faster read. type return won't allow checking return display?
because, type() returns class object, not string name of class , if below, work -
>>> if type('abcd') == str: ... print("blah") ... blah >>> type('abcd') <class 'str'> as note above, checked return of type('abcd') against str class , not string 'str' .
if want string representation of class, use tpye(<something>).__name__ , string name of class, though not needed case, information. example -
>>> type('abcd').__name__ 'str'
Comments
Post a Comment