Pythonでライブオブジェクトを検査する
このモジュールの関数は、モジュール、クラス、メソッド、関数、コードオブジェクトなどのライブオブジェクトに関する有用な情報を提供します。これらの関数は、型チェックを実行し、ソースコードを取得し、クラスと関数を検査し、インタープリタースタックを調べます。
getmembers()-この関数は、名前のリスト内のオブジェクトのすべてのメンバー、名前でソートされた値のペアを返します。オプションの述部が指定されている場合、述部が真の値を返すメンバーのみが含まれます。 getmodulename()-この関数は、ファイルパスで指定されたモジュールの名前を、囲んでいるパッケージの名前を含めずに返します
検査モジュールの動作を理解するために、次のスクリプトを使用します。
#inspect-example.py
'''This is module docstring'''
def hello():
'''hello docstring'''
print ('Hello world')
return
#class definitions
class parent:
'''parent docstring'''
def __init__(self):
self.var='hello'
def hello(self):
print (self.var)
class child(parent):
def hello(self):
'''hello function overridden'''
super().hello()
print ("How are you?") '__'で始まる
>>> import inspect, inspect_example
>>> for k,v in inspect.getmembers(inspect_example):
if k.startswith('__')==False:print (k,v)
child
hello
parent
>>> 述語
述語は、検査モジュールの関数に適用される論理条件です。たとえば、getmembers()関数は、指定された述語条件がtrueであるモジュールのメンバーのリストを返します。次の述語は、検査モジュールで定義されています
| ismodule() | オブジェクトがモジュールの場合はtrueを返します。 |
| isclass() | オブジェクトがクラスである場合、組み込みであるかPythonコードで作成されているかにかかわらず、trueを返します。 |
| ismethod() | オブジェクトがPythonで記述されたバインドされたメソッドの場合、trueを返します。 |
| isfunction() | オブジェクトがPython関数であり、ラムダ式によって作成された関数を含む場合はtrueを返します。 |
| isgenerator() | オブジェクトがジェネレーターの場合はtrueを返します。 |
| iscode() | オブジェクトがコードの場合はtrueを返します。 |
| isbuiltin() | オブジェクトが組み込み関数またはバインドされた組み込みメソッドの場合はtrueを返します。 |
| isabstract() | オブジェクトが抽象基本クラスの場合はtrueを返します。 |
ここでは、モジュール内のクラスメンバーのみが返されます。
>>> for k,v in inspect.getmembers(inspect_example, inspect.isclass):
print (k,v)
child <class 'inspect_example.child'>
parent <class 'inspect_example.parent'>
>>> 指定されたクラスのメンバーを取得するには'子'-
>>> inspect.getmembers(inspect_example.child) >>> x=inspect_example.child() >>> inspect.getmembers(x)
getdoc()関数は、モジュール、クラス、または関数のdocstringを取得します。
>>> inspect.getdoc(inspect_example) 'This is module docstring' >>> inspect.getdoc(inspect_example.parent) 'parent docstring' >>> inspect.getdoc(inspect_example.hello) 'hello docstring'
getsource()関数は、関数の定義コードをフェッチします-
>>> print (inspect.getsource(inspect_example.hello))
def hello():
'''hello docstring'''
print ('Hello world')
return
>>> sign=inspect.signature(inspect_example.parent.hello)
>>> print (sign) 検査モジュールには、コマンドラインインターフェイスもあります。
C:\Users\acer>python -m inspect -d inspect_example Target: inspect_example Origin: C:\python36\inspect_example.py Cached: C:\python36\__pycache__\inspect_example.cpython-36.pyc Loader: <_frozen_importlib_external.SourceFileLoader object at 0x0000029827BD0D30>
次のコマンドは、モジュール内の「Hello()」関数のソースコードを返します。
C:\Users\acer>python -m inspect inspect_example:hello
def hello():
'''hello docstring'''
print ('Hello world')
return -
Python Hello World:ハウツーガイド
Pythonの「HelloWorld」プログラムは通常、コーダーがPythonで作成する最初のプログラムです。このプログラムは、printステートメントを使用してPythonコンソールに文字列を表示します。プログラムは次のようになります:print(“ Hello World”)。 Pythonバージョンが機能することを確認するために作成できる最初のプログラムの1つは、「HelloWorld」です。プログラムを作成するには、ターミナルまたは選択したコードエディター(Visual Studio Code、Vimなど)の2つの方法があります。 開始するには、マシンにPython3がインストー
-
Pythonのファイルオブジェクト?
Pythonでは、ファイルの読み取りまたは書き込みを試みるたびに、ライブラリがネイティブに処理されるため、ライブラリをインポートする必要はありません。 最初に行うことは、組み込みのopen関数を使用してファイルオブジェクトを取得することです。 open関数はファイルを開き、ファイルオブジェクトを返します。ファイルオブジェクトには、情報を取得したり、開いたファイルを操作したりするために使用できるメソッドと属性が含まれています。 ファイルとは何ですか? ファイルに対して操作を行う前に、まずファイルとは何かを理解しましょう。ファイルは、関連情報を保存するためのディスク上の名前付きの場所です。フ