2011-02-09 9 views
0

これは何をするのですか? オプションの引数のいずれかを与える:(、2 'ファイルを上書きしてOK?')ask_ok
Python - Noob質問 - 「オプション引数の1つを与える」とはどういう意味ですか?

def ask_ok(prompt, retries=4, complaint='Yes or no, please!'): 
    while True: 
     ok = input(prompt) 
     if ok in ('y', 'ye', 'yes'): 
      return True 
     if ok in ('n', 'no', 'nop', 'nope'): 
      return False 
     retries = retries - 1 
     if retries < 0: 
      raise IOError('refusenik user') 
     print(complaint) 

参照チュートリアル:
http://docs.python.org/py3k/tutorial/controlflow.html#default-argument-values

答えて

2

セット2からretriesとのcomplaintを残しデフォルト値 'はい、いいえ、してください!'関数定義の最初の行にあるオプション引数の順序は重要です。

+0

ありがとうございます。 – Josh

1

これは機能です。

あなたがPythonを学んでいるなら、まず「Learning Python」のような良い本を読むことをお勧めします。簡単なチュートリアルから始め、たくさん読んでください。読んだ後、多くのことを読む。 Pythonはプログラミングを開始するためのビートフルな言語です。時には、私のように、あなたは他の言語のものになりますが、Pythonにとどまり、良い結果を得ることができます。 Pythonで

+0

Pythonチュートリアルで何が問題になっていますか? – Josh

+0

Pythonチュートリアルで何も問題ありません。 Pythonのドキュメントとチュートリアルは、世界で最も優れたリソースです。 しかし、(何かを)学ぶことは、彼らが何らかの論理に従うので、より良いものになります。ユーザーが推奨するhttp://stackoverflow.com/booksで見つけることができます。 「Learning Python」をお勧めします。良いレッスンの後、ドキュメントを楽しんでください!あなたはチュートリアルで学ぶことができますが、私の意見では本が優れています。 – macm

4

引数のいくつかのタイプがある:

名前および任意

  • 位置及びキーワード
  • は、Pythonにおける関数の引数は、つまり、引数が割り当てられ、属性操作であります関数のローカル名前空間内の変数に渡します。

    あなたはこのような宣言がある場合:

    def some_func(pos_arg1, pos_arg2, kw_arg1=1, kw_arg2='test'): 
        print "postional arg 1 =", pos_arg1 
        print "postional arg 2 =", pos_arg2 
        print "keyword arg 1 =", kw_arg1 
        print "keyword arg 2 =", kw_arg2 
    

    位置引数は必須で、与えられた順に割り当てられますが、キーワード引数はオプションであり、任意の順序で呼び出すことができます - 省略し、名前のキーワード引数は宣言されたデフォルト値(この例では1と 'test')を仮定します。これまでのところ:

    >>> some_func(1) # positional arguments are mandatory 
    Traceback (most recent call last): 
        File "<interactive input>", line 1, in <module> 
    TypeError: some_func() takes at least 2 arguments (1 given) 
    >>> some_func(1, 2) # this is ok 
    postional arg 1 = 1 
    postional arg 2 = 2 
    keyword arg 1 = 1 
    keyword arg 2 = test 
    >>> some_func(1, 2, 3) # this is also ok, keyword args may work like positional 
    postional arg 1 = 1 
    postional arg 2 = 2 
    keyword arg 1 = 3 
    keyword arg 2 = test 
    >>> some_func(1, 2, 3, 4) # this is also ok, keyword args may work like positional 
    postional arg 1 = 1 
    postional arg 2 = 2 
    keyword arg 1 = 3 
    keyword arg 2 = 4 
    >>> some_func(1, 2, kw_arg2=3) # kyword arguments may be given in any order 
    postional arg 1 = 1 
    postional arg 2 = 2 
    keyword arg 1 = 1 
    keyword arg 2 = 3 
    

    あり、宣言されていない引数に問題がある:

    >>> some_func(1, 2, 3, 4, 5) 
    Traceback (most recent call last): 
        File "<interactive input>", line 1, in <module> 
    TypeError: some_func() takes at most 4 arguments (5 given) 
    

    は、しかし、あなたは特別なフォーム***を使用して任意の数の引数を持つことができます。

    >>> def some_func(pos_arg1, pos_arg2, *args, **kw_args): 
    ...  print "postional arg 1 =", pos_arg1 
    ...  print "postional arg 2 =", pos_arg2 
    ...  print "other positional orgs =", args 
    ...  print "other keyword args =", kw_args 
    ... 
    >>> some_func(1, 2, 3, 4, 5) # any number of arguments 
    postional arg 1 = 1 
    postional arg 2 = 2 
    other positional orgs = (3, 4, 5) 
    other keyword args = {} 
    >>> some_func(1, 2, a=3, x=4, y=5) # * and ** are optional 
    postional arg 1 = 1 
    postional arg 2 = 2 
    other positional orgs =() 
    other keyword args = {'a': 3, 'x': 4, 'y': 5} 
    >>> some_func(1, 2, 'banana', 'orange', 'apple', a=3, x=4, y=5) 
    postional arg 1 = 1 
    postional arg 2 = 2 
    other positional orgs = ('banana', 'orange', 'apple') 
    other keyword args = {'a': 3, 'x': 4, 'y': 5} 
    >>> 
    

    *引数は位置指定引数のタプルとして使用でき、**はキーワード引数のdictになります。

    すべてを組み合わせることはできますが、すべてのキーワード引数は、定位置引数の後に宣言する必要があり、すべての任意の引数は名前付き引数の後になければなりません。

+0

非常に有益な回答ありがとうございます。 – Josh

+0

なぜ少なくとも2つの引数を与える必要がありますか? – Josh

+0

名前付き位置引数は必須であるためです。 'def some_func(* args)'でオプションにすることができます。 –

関連する問題