コメントの記法、クラスやメソッドの詳細参照等
コメント
# 1行コメント
"""
複数行コメント
といっても文法的にあるわけではなく、複数行文字列を置いてるだけ
"""
def test():
# シャープはインデントしなくてもOK
"""
こちらは文法上、インデントしないとNG
"""
対象クラスのメソッドやフィールドの一覧を表示
# sysのフィールドとメソッド名を出力してみる import sys print ( dir( sys ) ) # => ['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'gettrace', 'getwindowsversion', 'hexversion', 'int_info', 'intern', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'setcheckinterval', 'setfilesystemencoding', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions', 'winver']
helpの参照
# helpを使用すると、そのクラスのhelpが閲覧出来る help( "str" ) #=> クラスの説明と、各メソッドの使用方法等 help( "str".join ) #=> メソッドを指定すれば、各メソッドごとのヘルプを閲覧可能
__doc__の参照と設定
__doc__を使うと、各クラスやメソッドの概要を閲覧出来る
# stringクラスのdocを見てみる
print( "str".__doc__ )
#=>
"""
str(string[, encoding[, errors]]) -> str
Create a new string object from the given encoded string.
encoding defaults to the current default string encoding.
errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'.
"""
# stringクラスのcapitalizeメソッドのdocを見てみる
print( "str".capitalize.__doc__ )
#=>
"""
S.capitalize() -> str
Return a capitalized version of S, i.e. make the first character
have upper case.
"""
# __doc__の設定
class TestClass:
"""This is TestClass.
"""
def testMethod(self):
"""This is testMethod
"""
print( "hello" )
# comment2
"""comment3
"""
print( TestClass.__doc__ )
#=> This is TestClass.
print( TestClass.testMethod.__doc__ )
#=> This is testMethod

