Python勉強中
ちょっとPythonが面白そうなんで今更ながら勉強中。
忘れない内に書いとく。
>>> def f(): ... pass ... >>> type(f) <type 'function'> >>> type(list) <type 'type'>
便利だ。
>>> dir(f) ['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__get__', '__getattribute__', '_ _hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr __', '__setattr__', '__str__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_d oc', 'func_globals', 'func_name']
めっちゃ便利やね。
これは良いね〜「type()」で、その実態がわかり、「dir()」で、利用できるファンクション?関数?属性?なんて言えば良いのか分からないが、とにかく便利だ。
>>> getattr(f, 'func_doc') >>> getattr(f, 'aaa') Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'function' object has no attribute 'aaa'
「getattr()」は「f」に「func_doc」があるかどうかの確認?
>>> callable(getattr(f, '__init__')) True >>> callable(getattr(f, '__doc__')) False
関数としてCALLできるかどうかの確認
>>> f.__init__() >>> f.__doc__() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'NoneType' object is not callable
便利便利〜だすな〜