2011-07-12 8 views
0

問題が発生しました。私はランダムな要素の配列を持って、私はオブジェクトを持っています(最大配列=オブジェクト内の最大attr)。可変数のattrで宣言されたPython

breadcrumbs = breadObject(element[0],element[1],element[2]) 

をしかし、私は唯一の2つの要素を持つ配列を持っている場合([0] [1])私はエラーを得た:しかし、私は使用している場合。 string_breadが元である

exec("breadcrumbs = breadObject(%s)"%string_bread) 
    return breadObject 

は私がしてみてください。 STR(「要素1」、「要素2」)が、それはエラーを返す:

name 'breadObject' is not defined

+0

あなたの質問は非常にあります混乱している。あなたは何を達成しようとしていますか? 'breadObject'はどのように定義されていますか?あなたがそこで試しているこの暗い 'exec'-voodooは何ですか?私はあなたが何かを試す前に[チュートリアル](http://docs.python.org/tutorial/)を読むべきだと思います - あなたは基本が不足しているようです。 –

+0

なぜbreadcrumbsではなく、breadObject'を返すのですか? breadcrumbs = breadObject(eval(string_bread)) 'を試してみたらどうなりますか? @ Space_C0wb0y:私の解釈が正しいなら、彼は文字列としての彼の関数の引数を得ているように見えます。しかし、これは主な点から分かれています。つまり、議論の数に基づいてエラーが発生しています。 user840724、Space_C0wb0yリクエストとしてオブジェクト定義を投稿できますか?または少なくとも 'breadObject'の' __init__'を投稿してください。 – JAB

答えて

2

ない正しくあなたを理解していますが、次の構文を使用することができると思うことを確認してください:

breadcrumbs = breadObject(*element) 

Arbitrary number of arguments can be collected with *args syntax and an arbitrary number of keyword arguments can be collected as a dictionary with **kwargs syntax.:

def function(*args, **kwargs): 
    assert isinstance(args, tuple), 'args is always a tuple' 
    assert isinstance(kwargs, dict), 'kwargs is always a dictionary' 

*args and **kwargs can be used to call functions with multiple arguments/keyword arguments from a tuple or dictionary

関連する問題