第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

“'int'對象沒有屬性'變量'”的常見原因?

“'int'對象沒有屬性'變量'”的常見原因?

狐的傳說 2021-07-30 15:36:54
看到有關(guān) Stack Overflow 的許多問題'int' object has no attribute 'variable'僅針對此類主題錯誤消息的特定場景過于具體,我想知道在涉及此類錯誤消息的任何給定場景中我需要重新檢查的代碼方面。簡單地說,這個網(wǎng)絡(luò)上沒有一個問題要求對所述錯誤消息進行廣泛的推理,我想這里有相當(dāng)多的讀者更愿意研究這些類型的問題,因為這是一種常見的情況這里的用戶提出了同樣的問題,但問題和答案對于該特定領(lǐng)域過于具體而沒有任何用處??偟膩碚f,該站點上的許多類似問題都過于針對某人代碼的特定上下文。我想確?;卮疬@個問題的答案幾乎可以回答有關(guān)所述錯誤消息的任何給定情況。對于這樣一個常見問題,我希望'int' object has no attribute variable在這里解決大部分問題。
查看完整描述

1 回答

?
catspeake

TA貢獻1111條經(jīng)驗 獲得超0個贊

對于這樣一個常見問題,我希望大多數(shù)“int”對象沒有屬性變量問題要在這里解決。


這是我的嘗試。首先,這不是最好的表征:


'int' object has no attribute 'variable'

由于我看到的大多數(shù)示例都是以下形式:


'int' object has no attribute 'method'

并且是由調(diào)用int未實現(xiàn)的方法引起的int:


>>> x = 4

>>> x.length()

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

AttributeError: 'int' object has no attribute 'length'

>>> 

該int班確實有方法:


>>> dir(int)

['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__',

'__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__',

'__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__',

'__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__',

'__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__',

'__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__',

'__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__',

'__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__',

'__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__',

'__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator',

'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']

>>> 

你可以打電話給他們:


>>> help(int.bit_length)

Help on method_descriptor:


bit_length(...)

    int.bit_length() -> int


    Number of bits necessary to represent self in binary.

    >>> bin(37)

    '0b100101'

    >>> (37).bit_length()

    6


>>> 

這向我們展示了如何在int不與小數(shù)點混淆的情況下調(diào)用 an 方法:


>>> (128).bit_length()

8

>>>

但在大多數(shù)情況下,并不是有人試圖在 an 上調(diào)用方法,int而是 anint是針對另一種對象類型的消息的錯誤接收者。例如,這是一個常見錯誤:


TypeError: 'int' object has no attribute '__getitem__'

當(dāng)您嘗試對 an 進行下標時,會出現(xiàn)在 Python2 中int:


>>> x = 4

>>> x[0]

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: 'int' object has no attribute '__getitem__'

>>> 

Python3 提供了更有用的信息,TypeError: 'int' object is not subscriptable.


如果您重用相同的變量名來保存不同類型的數(shù)據(jù),有時會發(fā)生這種情況——應(yīng)避免這種做法。


如果您收到類似的錯誤"AttributeError: 'int' object has no attribute 'append'",請考慮響應(yīng)什么類型的對象append()。Alist是,所以在我的代碼中的某個地方我調(diào)用append()了一個int我認為我有一個list.


查看完整回答
反對 回復(fù) 2021-08-03
  • 1 回答
  • 0 關(guān)注
  • 503 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號