os.pipe()関数はPythonで何をしますか?
メソッドos.pipe()はパイプを作成し、それぞれ読み取りと書き込みに使用できるファイル記述子のペア(r、w)を返します。
例
import os, sys
print "The child will write text to a pipe and "
print "the parent will read the text written by child..."
# file descriptors r, w for reading and writing
r, w = os.pipe()
processid = os.fork()
# This is the parent process
if processid:
os.close(w)
r = os.fdopen(r)
print "Parent reading"
str = r.read()
print "text =", str
sys.exit(0)
else:
# This is the child process
os.close(r)
w = os.fdopen(w, 'w')
print "Child writing"
w.write("Text written by child...")
w.close()
print "Child closing"
sys.exit(0) 出力
出力が表示されます:
Parent reading Child writing Child closing text = Text written by child...
-
Pythonの名前空間とは何ですか?
名前空間は、スコープを実装する方法です。 Pythonでは、各パッケージ、モジュール、クラス、関数、およびメソッド関数は、変数名が解決される「名前空間」を所有しています。関数、モジュール、またはパッケージが評価される(つまり、実行が開始される)と、名前空間が作成されます。それを「評価コンテキスト」と考えてください。関数などの実行が終了すると、名前空間は削除されます。変数は削除されます。さらに、名前がローカル名前空間にない場合に使用されるグローバル名前空間があります。 各変数名はローカル名前空間(関数の本体、モジュールなど)でチェックされ、次にグローバル名前空間でチェックされます。 変数は通
-
%はPythonの文字列に何をしますか?
%は、文字列フォーマット演算子または補間演算子です。 formatの%値(formatは文字列)を指定すると、formatの%変換仕様は、0個以上の値の要素に置き換えられます。この効果は、C言語でsprintf()を使用する場合と同様です。たとえば、 >>> lang = "Python" >>> print "%s is awesome!" % lang Python is awesome この表記で数値をフォーマットすることもできます。たとえば、 >>> cost = 128.527 >&