この記事では、主に Python の __name__ 値のテストの詳細な紹介を紹介します
テストで使用されるコードは次のとおりです:
#test_name0.py def test(): return name print name print test() import test_name1 test_name1.test()
#test_name1.py def test(): print name print name
pythonトップレベルのインタープリターでコマンドを実行すると、結果は次のようになります:
In [1]: type(name) Out[1]: str In [2]: print name Out[2]: main In [3]: import test_name0 test_name0 test_name0 test_name1 test_name1
cmd で の <code>python test_name0.py
を実行すると、結果は次のようになります: python test_name0.py
,结果如下:
main main test_name1 test_name1
由此可以看出:
(1)在python顶层解释器或直接运行的脚本中name=='main'
(2)在调用的模块中name==<a href="http://www.php.cn/code/8212.html" target="_blank">module</a> name
(3)发现一个非预期的情况,就是在运行了test_name0.py
的脚本后再import test_name0
,或者import test_name0
后再运行test_name0.py
test_name0 test_name0 test_name1これからわかります: (1)
name== 'Python のトップレベル インタプリタ内の main またはスクリプトを直接実行します '
(2) 呼び出されたモジュール内 name==<a href="http://www.php.cn/code/%208212.html" target="_blank">モジュール<p> 名</p></a>
(3) 予期しない状況が見つかりました。つまり、test_name0.py
スクリプトを実行した後、import test_name0
、または import test_name0
の後に test_name0.py
スクリプトを実行すると、結果は
main main test_name1または
test_name0 test_name0 test_name1 test_name1ではなく
main main test_name1 test_name1または rrreee になります。 .segmentfault で問題のキーを整理し、質問をしてこの問題が発生した理由を見つけました。 🎜つまり、Python モジュールには 🎜 キャッシュ🎜 があり、それを一度インポートしてから再度インポートした場合、モジュールの最上位スコープ内のコードは再度実行されません。 🎜🎜 (4) 2 つのモジュール test_name0 と test_name1 のテスト 🎜 関数 🎜 は同じ名前です。モジュール名が異なるため、2 つのテスト関数を区別できますが、モジュール名の重複は避けなければなりません。 。 🎜
以上がPython での __name__ 値のテストの詳細な紹介の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

Arraysinpython、特にvianumpy、arecrucialinscientificComputing fortheirefficienty andversitility.1)彼らは、fornumericaloperations、data analysis、andmachinelearning.2)numpy'simplementation incensuresfasteroperationsthanpasteroperations.3)arayableminablecickick

Pyenv、Venv、およびAnacondaを使用して、さまざまなPythonバージョンを管理できます。 1)Pyenvを使用して、複数のPythonバージョンを管理します。Pyenvをインストールし、グローバルバージョンとローカルバージョンを設定します。 2)VENVを使用して仮想環境を作成して、プロジェクトの依存関係を分離します。 3)Anacondaを使用して、データサイエンスプロジェクトでPythonバージョンを管理します。 4)システムレベルのタスク用にシステムPythonを保持します。これらのツールと戦略を通じて、Pythonのさまざまなバージョンを効果的に管理して、プロジェクトのスムーズな実行を確保できます。

numpyarrayshaveveraladvantages-averstandardpythonarrays:1)thealmuchfasterduetocベースのインプレンテーション、2)アレモレメモリ効率、特にlargedatasets、および3)それらは、拡散化された、構造化された形成術科療法、

パフォーマンスに対する配列の均一性の影響は二重です。1)均一性により、コンパイラはメモリアクセスを最適化し、パフォーマンスを改善できます。 2)しかし、タイプの多様性を制限し、それが非効率につながる可能性があります。要するに、適切なデータ構造を選択することが重要です。

craftexecutablepythonscripts、次のようになります

numpyarraysarasarebetterfornumeroperations andmulti-dimensionaldata、whilethearraymoduleissuitable forbasic、1)numpyexcelsinperformance and forlargedatasentassandcomplexoperations.2)thearraymuremememory-effictientivearientfa

NumPyArraySareBetterforHeavyNumericalComputing、whilethearrayarayismoreSuitableformemory-constrainedprojectswithsimpledatatypes.1)numpyarraysofferarays andatiledance andpeperancedatasandatassandcomplexoperations.2)thearraymoduleisuleiseightweightandmemememe-ef

ctypesallowsinging andmanipulatingc-stylearraysinpython.1)usectypestointerfacewithclibrariesforperformance.2)createc-stylearraysfornumericalcomputations.3)passarraystocfunctions foreffientientoperations.how、how、becuutiousmorymanagemation、performanceo


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

Dreamweaver Mac版
ビジュアル Web 開発ツール

VSCode Windows 64 ビットのダウンロード
Microsoft によって発売された無料で強力な IDE エディター

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

Safe Exam Browser
Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

ドリームウィーバー CS6
ビジュアル Web 開発ツール

ホットトピック









