sarathのmadala:
変数に依存すると仮定するのために私は、いくつかのクラスをインポートし、そのオブジェクトを作成し、それを返すようにしたいです。例えば :
if x=='SomeThing':
import something
object = something's object
else:
import nothing
object = nothing's object
object.function()
私はこれを行うことができますどのようにラムダを使用して、上記やりたいですか?
chepner:
めったに単にインポートする問題ではないの両方無条件のモジュールを、そして実際にどのモジュールを選択し使用後に。
import something
import nothing
(something if x == 'SomeThing' else nothing).object.function()
あなたは場合か、インポートどちらか一方、条件付きのインポートを実行する必要がありますが、使用して同じ名前を。
if x == 'SomeThing':
import something as thingmodule
else:
import nothing as thingmodule
thingmodule.object.function()