[英]Can't understand size_hint. Why this .kv simply code doesn't work as i expected?
[英]Simply can't understand why self is not defined
import requests
valute = input('Enter currency name: ')
class Rate:
def __ init__ (self, format_):
format_ = self.format
def name_and_max_value(self, format_='full'):
"""
Return currency name (‘Name’) with its max value from site
"""
self.r = requests.get('https://www.cbr-xml-daily.ru/daily_json.js')
if self.r.json()["Value"] > self.r.json()["Previous"]:
return self.r.json()["Value"]
else:
return self.r.json()["Previous"]
Rate.name_and_max_value(self, format_='full')
錯誤正文:
輸入貨幣名稱:usd
NameError Traceback (most recent call last)
<ipython-input-30-4d5dab537669> in <module>()
17 return self.r.json()["Previous"]
18
---> 19 Rate.name_and_max_value(self, format_='full')
NameError: name 'self' is not defined
首先,您似乎希望name_and_max_value
成為classmethod
,因為您沒有創建實例。 如果是這種情況,您應該使用@classmethod
裝飾器明確說明。 這些方法的約定是調用第一個參數cls
,而不是self
。
其次,無論是 class 還是實例方法,都不要在調用中包含self
:這是系統自動傳遞的。
還要注意init_
永遠不會被執行:你可能想寫__init__
?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.