简体   繁体   English

无法从 class 打印 __str__

[英]Can't print __str__ from the class

I wondered how can I print the class contents of a particular index.我想知道如何打印特定索引的 class 内容。 I made a class that has all the values of a certain seismic movement and stores each in its own data type.我制作了一个 class,它具有某个地震运动的所有值,并将每个值存储在自己的数据类型中。

Here is the class:这是 class:

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))

I was trying to print the data using str method but it doesn't work.我试图使用str方法打印数据,但它不起作用。

Here is the error:这是错误:

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

My question is how to print the string i tried to print using str method in the class. Thanks very much beforehand.我的问题是如何打印我尝试使用 class 中的 str 方法打印的字符串。非常感谢。

Your immediate problem is that you shadowed all your methods with instance attributes, instead of using _ -prefixed names for the attributes that the method expect.您的直接问题是您使用实例属性隐藏了所有方法,而不是为方法期望的属性使用_前缀名称。

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]    

However, none of those getters are necessary;但是,这些吸气剂都不是必需的。 just use the instance attributes directly.直接使用实例属性即可。 In __str__ , use an f-string to perform any necessary type conversions (which you are currently doing wrong; non- str values need to be converted to str values, not the other way around).__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))

Finally, I would recommend making __init__ not be responsible for splitting up a list.最后,我建议让__init__负责拆分列表。 Have it simply take 10 different arguments, and use a dedicated class method to parse a list in a fixed format.让它只需要 10 个不同的 arguments,并使用专用的 class 方法来解析固定格式的列表。

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}'

I dont know why u created all these methods individualy but i found that your self variables have the exact same name as your functions does, so change your self variables name(also remeber to change the variables name where u have returned each of them and remove the dashes from the beginning), remove the '()' from the result list, also remove the 'float()' and 'int()' from the final return from the class and replace them with 'str()'我不知道您为什么单独创建所有这些方法,但我发现您的自变量与您的函数具有完全相同的名称,因此请更改您的自变量名称(还要记住更改您返回每个变量的变量名称并删除从头开始的破折号),从结果列表中删除 '()',还从 class 的最终返回中删除 '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))

I have corrected your code and this should be like this.我已经更正了您的代码,应该是这样的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM