2017-11-01 10 views
0

以下の機能では、チャットやチャット内のpim関数のパラメータをパラメータとして渡すことなく、それらをどのように使用するのですか?Pythonでパラメータとして渡さずに別の関数で関数変数を使う方法は?

base.py:

def pim(mode, tag, population_file, variable_file, aggregation, user, 
     passw, email, working_schema, output_schema): 

    print mode 
    print tag 
    print population_file 
    print variable_file 
    print aggregation 
    print user 
    print passw 
    print email 
    print working_schema 
    print output_schema 


    chat() 
    chat1() 

私は(chat()が作成された)chat.pyにでfrom base import *を使用してみましたが、それは認識されていません。パラメータとして渡すことなくパラメータにアクセスする方法があるかどうかを知りたいですか?

+1

あなたはグローバル変数のいくつかの種類を探しているようですね。あなたが求めているものがPythonにあるかどうかは確信していませんが、通常はお勧めしません。(コードベース内のどこでも特定の変数にアクセスして変更することができます。 [classes](https://docs.python.org/3/tutorial/classes.html#tut-classdefinition)にチェックを入れたい場合があります。たぶんあなたはこれらの変数をすべて一つのクラスに束縛し、 'chat()'と 'chat1()'関数に_that_を渡すことができます。 –

答えて

0

パラメータを渡すことを避ける理由は、血まみれの長いパラメータリストがあるということです。

あなたが実際に以下のことに機能pimを変更することができます。

def pim(*args): 
    print(args[0]) 
    print(args[1]) 
    print(args[2]) 
    print(args[3]) 
    chat(args) 
    chat1(args) 
# You can call this function by 
pim(mode,tag,population_file,variable_file,aggregation,user,passw,email,working_schema,output_schema) 

または次のようにオプションの引数の形式を使用することができます。

def pim(**kwargs): 
    print(kwargs['mode']) 
    print(kwargs['tag']) 
    print(kwargs['population_file']) 
    print(kwargs['variable_file']) 
    chat(kwargs) 
    chat1(kwargs) 
# and you can call your function by 
pim(mode='1', tag='2', 'population_file'=3, 'variable_file'=4) 

たり、パラメータを作るためにglobalキーワードを使用することができますしかし、このソリューションはお勧めできません。

0

あなたは、変数内の入力パラメータを格納し、変数はグローバルに、例えばすることができます

mode = "" 

def pim(mode,tag,population_file,variable_file,aggregation,user,passw,email,working_schema,output_schema): 

    globals()['mode'] = mode 

    chat() 
    chat1() 
+1

申し訳ありませんが、Swiftと混同されました。今すぐ変更しました –

関連する問題