鱼C论坛

 找回密码
 立即注册
查看: 1046|回复: 3

[已解决]如何像__repr__()一样输入实例名返回列表?

[复制链接]
发表于 2020-6-18 16:54:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Nmbh 于 2020-6-18 16:55 编辑

我写了段代码想实现《零基础学习python》第38讲动动手第0题的要求。
但是在代码中我用Point类中使用__repr__(),想实现但用户输入类的实例化名称时以列表形式返回[x,y]坐标,但发现__repr__和__str__一样只能返回字符串。
请问有没有什么魔法方法能够返回非字符串(比如数字、列表)

  1. import math as m
  2. class Point:
  3.       def __init__(self,x=0,y=0):
  4.             self.x = x
  5.             self.y = y
  6.       def __repr__(self):
  7.         return [self.x,self.y]

  8. class Line:
  9.       def __init__(self,point1 = [0,0], point2 = [0,0]):
  10.             self.point1 = point1
  11.             self.point2 = point2
  12.       
  13.       def getLen(self):
  14.             length = m.sqrt((self.point1[0]-self.point2[0])**2 + (self.point1[1]-self.point2[1])**2)
  15.             return length

  16. a = Point(1,1)
  17. b = Point(5,4)
  18. c = Line(a,b)
  19. c.getLen()
复制代码
最佳答案
2020-6-18 21:58:11
把这个:
  1. class Point:
  2.       def __init__(self,x=0,y=0):
  3.             self.x = x
  4.             self.y = y
  5.       def __repr__(self):
  6.         return [self.x,self.y]
复制代码

改成:
  1. class Point:
  2.       def __init__(self,x=0,y=0):
  3.             self.x = x
  4.             self.y = y
  5.       def __repr__(self):
  6.         return str([self.x,self.y])
复制代码

不就行了
123.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-6-18 17:09:37 | 显示全部楼层
分为两个情况,要么返回给人看,要么返回给程序看。对应的__str__和__repr__ 。都是字符串,重要你怎么展示这个字符串,

我看你的代码和你的问题没有任何关联,如果是问报错问题,你的Point没有实现__getitem__不能用索引取值,用point.x  point.y是取值
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-18 18:24:16 | 显示全部楼层
我试着用eval回避了__repr__()输出为字符串的问题
  1. import math as m
  2. class Point:
  3.       def __init__(self,x=0,y=0):
  4.             self.x = x
  5.             self.y = y
  6.       def __repr__(self):
  7.         return str([self.x,self.y])

  8. class Line:
  9.       def __init__(self,point1 = [0,0], point2 = [0,0]):
  10.             if not isinstance(point1,list):
  11.                 point1 = eval(str(point1))
  12.             if not isinstance(point2,list):
  13.                 point2 = eval(str(point2))            
  14.             self.point1 = point1
  15.             self.point2 = point2
  16.       
  17.       def getLen(self):
  18.             length = m.sqrt((self.point1[0]-self.point2[0])**2 + (self.point1[1]-self.point2[1])**2)
  19.             return length
  20. a = Point(1,1)
  21. b = Point(5,4)
  22. c = Line(a,b)
  23. c.getLen()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-18 21:58:11 | 显示全部楼层    本楼为最佳答案   
把这个:
  1. class Point:
  2.       def __init__(self,x=0,y=0):
  3.             self.x = x
  4.             self.y = y
  5.       def __repr__(self):
  6.         return [self.x,self.y]
复制代码

改成:
  1. class Point:
  2.       def __init__(self,x=0,y=0):
  3.             self.x = x
  4.             self.y = y
  5.       def __repr__(self):
  6.         return str([self.x,self.y])
复制代码

不就行了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-22 04:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表