我想知道如何列印特定索引的類內容。我創建了一個類,其中包含某個地震運動的所有值,并將每個值存盤在自己的資料型別中。
這是課程:
import re
class Txt_data:
def __init__(self, result):
self.date = result[0]
self.time = result[1]
self.latit = result[2]
self.long = result[3]
self.depth = result[4]
self.md = result[5]
self.ml = result[6]
self.mw = result[7]
self.region = result[8]
self.method = result[9]
def date(self):
return self._date
def time(self):
return self._time
def latit(self):
return self._latit
def long(self):
return self._long
def depth(self):
return self._depth
def md(self):
return self._md
def ml(self):
return self._ml
def mw(self):
return self._mw
def region(self):
return self._region
def method(self):
return self._method
# This does not work
def __str__(self):
return ('MAG: ' float(self.ml()) ' DEPTH: ' int(self.region()) ' DATE/TIME: ' str(self.date()) ' ' str(self.time()) ' LAT: ' float(self.latit()) ' LON: ' float(self.long()))
result = [('2021.12.02', '22:29:24', '36.9605', '28.1775', '13.0', '-.-', '1.5', '-.-', 'KARACA-MARMARIS (MUGLA)', ' Quick')]
print(Txt_data(result))
我試圖使用str方法列印資料,但它不起作用。
這是錯誤:
Traceback (most recent call last):
File "/Users/seyfalsultanov/Documents/uni comp 100/comp100-2021f-ps5-seyfalku/main.py", line 73, in <module>
print(Txt_data(result))
File "/Users/seyfalsultanov/Documents/uni comp 100/comp100-2021f-ps5-seyfalku/main.py", line 60, in __str__
print('MAG: ' float(self.ml()) ' DEPTH: ' int(self.region()) ' DATE/TIME: ' str(self.date()) ' ' str(self.time()) ' LAT: ' float(self.latit()) ' LON: ' float(self.long()))
AttributeError: Txt_data instance has no __call__ method
我的問題是如何列印我嘗試在類中使用 str 方法列印的字串。非常感謝。
uj5u.com熱心網友回復:
您的直接問題是您使用實體屬性_隱藏了所有方法,而不是為方法期望的屬性使用-prefixed 名稱。
def __init__(self, result):
self._date = result[0]
self._time = result[1]
self._latit = result[2]
self._long = result[3]
self._depth = result[4]
self._md = result[5]
self._ml = result[6]
self._mw = result[7]
self._region = result[8]
self._method = result[9]
然而,這些吸氣劑都不是必需的。直接使用實體屬性即可。在 中__str__,使用 f-string 執行任何必要的型別轉換(您目前做錯了;非str值需要轉換為str值,而不是相反)。
class Txt_data:
def __init__(self, result):
self.date = result[0]
self.time = result[1]
self.latit = result[2]
self.long = result[3]
self.depth = result[4]
self.md = result[5]
self.ml = result[6]
self.mw = result[7]
self.region = result[8]
self.method = result[9]
def __str__(self):
return f'MAG: {self.ml} DEPTH: {self.region} DATE/TIME: {self.date} {self.time} LAT: {self.latit} LON: {self.long}'
result = ('2021.12.02', '22:29:24', '36.9605', '28.1775', '13.0', '-.-', '1.5', '-.-', 'KARACA-MARMARIS (MUGLA)', ' Quick')
print(Txt_data(result))
最后,我建議 make__init__ 不負責拆分串列。讓它簡單地接受 10 個不同的引數,并使用專用的類方法來決議固定格式的串列。
class Txt_data:
def __init__(self, date, time, latitude, long, depth, md, ml, mw, region, method):
self.date = date
self.time = time
self.latit = latit
self.long = long
self.depth = depth
self.md = md
self.ml = ml
self.mw = mw
self.region = region
self.method = method
@classmethod
def from_list(cls, x):
if len(x) != 10:
raise ValueError("Wrong number of elements in list")
return cls(*x)
def __str__(self):
return f'MAG: {self.ml} DEPTH: {self.region} DATE/TIME: {self.date} {self.time} LAT: {self.latit} LON: {self.long}'
uj5u.com熱心網友回復:
我不知道你為什么單獨創建所有這些方法,但我發現你的 self 變數與你的函式具有完全相同的名稱,所以更改你的 self 變數名稱(還要記住更改你已經回傳的變數名稱并洗掉從頭開始的破折號),從結果串列中洗掉 '()',同時從類的最終回傳中洗掉 'float()' 和 'int()' 并將它們替換為 'str()'
import re
class Txt_data:
def __init__(self, result):
self.date1 = result[0]
self.time1 = result[1]
self.latit1 = result[2]
self.long1 = result[3]
self.depth1 = result[4]
self.md1 = result[5]
self.ml1 = result[6]
self.mw1 = result[7]
self.region1 = result[8]
self.method1 = result[9]
def date(self):
return self.date1
def time(self):
return self.time1
def latit(self):
return self.latit1
def long(self):
return self.long1
def depth(self):
return self.depth1
def md(self):
return self.md1
def ml(self):
return self.ml1
def mw(self):
return self.mw1
def region(self):
return self.region1
def method(self):
return self.method1
def __str__(self):
return ('MAG: ' str(self.ml()) ' DEPTH: ' str(self.region()) ' DATE/TIME: ' str(self.date()) ' ' str(self.time()) ' LAT: ' str(self.latit()) ' LON: ' str(self.long()))
result = ['2021.12.02', '22:29:24', '36.9605', '28.1775', '13.0', '-.-', '1.5', '-.-', 'KARACA-MARMARIS (MUGLA)', ' Quick']
print(Txt_data(result))
我已經更正了你的代碼,這應該是這樣的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/403153.html
標籤:
上一篇:讀書筆記《Deep Learning for Computer Vision with Python》- 第三卷 第13章 Faster R-CNNs
下一篇:從同一個父類的另一個類訪問物件
