どのプログラミング言語でも、新しいプログラムを開発すると、エラーや例外が発生する可能性が高くなります。これらのエラーは、プログラムが実行されていないことを示します。 Python でよく発生するエラーの 1 つは AttributeError です。 AttributeError は、属性の参照または割り当てが失敗したときに発生するエラーとして定義できます。
たとえば、変数 x を取得すると、値 10 が割り当てられます。このプロセスでは、その変数に別の値を追加するとします。不可能です。変数は整数型であるため、append メソッドはサポートされません。したがって、この種の問題では、AttributeError と呼ばれるエラーが発生します。変数がリスト型の場合、append メソッドをサポートするとします。その後、問題はなく、getAttribute エラーは発生しません。
注記: Python の属性エラーは通常、無効な属性参照が行われたときに発生します。
AttributeError が発生する可能性がいくつかあります。
例 1:
Python3
# Python program to demonstrate> # AttributeError> X>=> 10> # Raises an AttributeError> X.append(>6>)> |
>
>
出力:
Traceback (most recent call last): File '/home/46576cfdd7cb1db75480a8653e2115cc.py', line 5, in X.append(6) AttributeError: 'int' object has no attribute 'append'>
例 2: Python は大文字と小文字を区別する言語であるため、スペルが異なると属性エラーが発生することがあります。
Python3
# Python program to demonstrate> # AttributeError> # Raises an AttributeError as there is no> # method as fst for strings> string>=> 'The famous website is { }'>.fst(>'geeksforgeeks'>)> print>(string)> |
tostringメソッドJava
>
>
出力:
Traceback (most recent call last): File '/home/2078367df38257e2ec3aead22841c153.py', line 3, in string = 'The famous website is { }'.fst('geeksforgeeks') AttributeError: 'str' object has no attribute 'fst'> 例 3 注: ユーザーが無効な属性参照を作成しようとしたときに、ユーザー定義クラスに対して AttributeError が発生することもあります。
Python3
# Python program to demonstrate> # AttributeError> class> Geeks():> > >def> __init__(>self>):> >self>.a>=> 'techcodeview.com'> > # Driver's code> obj>=> Geeks()> print>(obj.a)> # Raises an AttributeError as there> # is no attribute b> print>(obj.b)> |
>
>
出力:
techcodeview.com>
エラー:
Traceback (most recent call last): File '/home/373989a62f52a8b91cb2d3300f411083.py', line 17, in print(obj.b) AttributeError: 'Geeks' object has no attribute 'b'>
例 4: AttributeError は、ユーザーがコード行間にタブやスペースを追加し忘れた場合にも、ユーザー定義クラスに対して発生する可能性があります。
Python3
#This is a dictionary parsing code written by Amit Jadhav> #Because of an Indentation Error you will experience Attribute Error> class> dict_parsing:> > >def> __init__(>self>,a):> >self>.a>=> a> > >def> getkeys(>self>):> >if> self>.notdict():> >return> list>(>self>.a.keys())> > >def> getvalues(>self>):> >if> self>.notdict():> >return> list>(>self>.a.values())> > >def> notdict(>self>):> >if> type>(>self>.a) !>=> dict>:> >raise> Exception(>self>,a,>'not a dictionary'>)> >return> 1> > >def> userinput(>self>):> >self>.a>=> eval>(>input>())> >print>(>self>.a,>type>(>self>.a))> >print>(>self>.getykeys())> >print>(>self>.getvalyes())> > >def> insertion(>self>,k,v):> >self>.a[k]>=>v> > d>=> dict_parsing({>'k1'>:>'amit'>,>'k2'>:[>1>,>2>,>3>,>4>,>5>]})> d.getkeys()> |
>
>
出力:
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) in ---->1 d.getkeys() 属性エラー: 'dict_parsing' オブジェクトには属性 'getkeys' がありません>
エラー:
Javaでcatchブロックを試してください
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) in ---->1 d.getkeys() 属性エラー: 'dict_parsing' オブジェクトには属性 'getkeys' がありません>
AttributeError の解決策
Python のエラーと例外は、例外処理を使用して処理できます。つまり、Python では try と else を使用します。
例: 上記のクラスの例を考えてみましょう。AttributeError が発生するたびにトレースバックを出力するのではなく、別のことを行いたいと考えています。
Python3
# Python program to demonstrate> # AttributeError> class> Geeks():> > >def> __init__(>self>):> >self>.a>=> 'techcodeview.com'> # Driver's code> obj>=> Geeks()> # Try and except statement for> # Exception handling> try>:> >print>(obj.a)> > ># Raises an AttributeError> >print>(obj.b)> > # Prints the below statement> # whenever an AttributeError is> # raised> except> AttributeError:> >print>(>'There is no such attribute'>)> |
>
>
出力:
techcodeview.com There is no such attribute>
注記: 例外処理の詳細については、ここをクリックしてください。