型判別とか その2
昨日の日記で書いて気になりだしたので少し詳しく調べてみた。
強いて分類すると
リテラルっぽいのは
- NoneType(None)
- BooleanType(True, False)
- IntType, LongType, FloatTypeなどの数字
- StringType, UnicodeTypeなどの文字列
意外?にも全て__class__アトリビュートがあったりする。
int(), str()あたりも知っていると便利。
コンテナ類は
- TupleType, ListType, DictionaryTypeなど
クラス類は
このあたりの判別にis演算子とかisinstance()を使う
関数類に
- FunctionType, BuiltinFunctionType, BuiltinMethodTypeなど
そのほかに
- TypeType
- ModuleType
なんかがあった。
すっきりしていないということが納得できたw
実験に使ったスクリプト
class LegacyClass: def __init__(self): self.property=1 class NewStyleClass: def __init__(self): self.property=1 def generator(): count=0 while True: yield count count+=1 def analyse(obj): print obj obj_type=type(obj) print obj_type try: print obj.__class__ except AttributeError, e: print e # types if obj_type is types.NoneType: print "is NoneType" if obj_type is types.TypeType: print "is TypeType" if obj_type is types.BooleanType: print "is BooleanType" if obj_type is types.IntType: print "is IntType" if obj_type is types.LongType: print "is LongType" if obj_type is types.FloatType: print "is FloatType" if obj_type is types.ComplexType: print "is ComplexType" if obj_type is types.StringType: print "is StringType" if obj_type is types.UnicodeType: print "is UnicodeType" if obj_type is types.TupleType: print "is TupleType" if obj_type is types.ListType: print "is ListType" if obj_type is types.DictType: print "is DictType" if obj_type is types.DictionaryType: print "is DictionaryType" if obj_type is types.FunctionType: print "is FunctionType" if obj_type is types.LambdaType: print "is LambdaType" if obj_type is types.GeneratorType: print "is GeneratorType" if obj_type is types.CodeType: print "is CodeType" if obj_type is types.ClassType: print "is ClassType" if obj_type is types.InstanceType: print "is InstanceType" if obj_type is types.MethodType: print "is MethodType" if obj_type is types.UnboundMethodType: print "is UnboundMethodType" if obj_type is types.BuiltinFunctionType: print "is BuiltinFunctionType" if obj_type is types.BuiltinMethodType: print "is BuiltinMethodType" if obj_type is types.ModuleType: print "is ModuleType" if obj_type is types.FileType: print "is FileType" if obj_type is types.XRangeType: print "is XRangeType" if obj_type is types.SliceType: print "is SliceType" if obj_type is types.EllipsisType: print "is EllipsisType" if obj_type is types.TracebackType: print "is TracebackType" if obj_type is types.FrameType: print "is FrameType" if obj_type is types.BufferType: print "is BufferType" if obj_type is types.DictProxyType: print "is DictProxyType" if obj_type is types.NotImplementedType: print "is NotImplementedType" if obj_type is types.GetSetDescriptorType: print "is GetSetDescriptorType" if obj_type is types.MemberDescriptorType: print "is MemberDescriptorType" if obj_type is types.StringTypes: print "is StringTypes" # inspect if inspect.ismodule(obj): print "is module" if inspect.isclass(obj): print "is class" if inspect.ismethod(obj): print "is method" if inspect.isfunction(obj): print "is function" if inspect.istraceback(obj): print "is traceback" if inspect.isframe(obj): print "is frame" if inspect.iscode(obj): print "is code" if inspect.isbuiltin(obj): print "is builtin" if inspect.isroutine(obj): print "is routine" if inspect.ismethoddescriptor(obj): print "is method descriptor" if inspect.isdatadescriptor(obj): print "is data descriptor" if inspect.isgetsetdescriptor(obj): print "is get set descriptor" if inspect.ismemberdescriptor(obj): print "is member descriptor" print if __name__=="__main__": import types import inspect import sys analyse(1) analyse(.1) analyse(None) analyse(True) analyse(False) analyse(["list"]) analyse(("tu", "ple")) analyse({"dict":"ionary"}) analyse("string") analyse(u"unicode") analyse(analyse) analyse(xrange(2)) analyse(generator) analyse(reversed([])) analyse(lambda x:x+x) analyse(dir) analyse(LegacyClass) analyse(LegacyClass()) analyse(NewStyleClass) analyse(NewStyleClass()) analyse(types) analyse(sys) analyse(int) analyse(str) analyse(list) analyse(dict)