繁体   English   中英

从 object 方法打印 output [重复]

[英]printing an output from an object method [duplicate]

我希望 output 成为 x 变量。 我的代码有什么问题。

class Point:
    def __init__(self,x,y):
        self.x=x
        self.y=y
    
    def getX(self):
        return self.x

# create a list of the objects
L = [Point(1,2), Point(3,4), Point(2,5), Point(6,4), Point(8,6), Point(9,2), Point(10,4)]

sortedL=sorted(L, key=Point.set_priority)

print(sortedL)

for i in sortdL:
    print(i.getX)

结果如下:

[<__main__.Point object at 0x000001EB46A9CFD0>, <__main__.Point object at 0x000001EB46A9CD30>, <__main__.Point object at 0x000001EB46A9CF70>, <__main__.Point object at 0x000001EB46A9CC70>, <__main__.Point object at 0x000001EB46A9CC10>, <__main__.Point object at 0x000001EB46A9CBB0>, <__main__.Point object at 0x000001EB46A9CB50>]
<bound method Point.getX of <__main__.Point object at 0x000001EB46A9CFD0>>
<bound method Point.getX of <__main__.Point object at 0x000001EB46A9CD30>>
<bound method Point.getX of <__main__.Point object at 0x000001EB46A9CF70>>
<bound method Point.getX of <__main__.Point object at 0x000001EB46A9CC70>>
<bound method Point.getX of <__main__.Point object at 0x000001EB46A9CC10>>
<bound method Point.getX of <__main__.Point object at 0x000001EB46A9CBB0>>
<bound method Point.getX of <__main__.Point object at 0x000001EB46A9CB50>>

我是一个初学者。 我尝试print(i.getX())并且它有效,所以任何人都可以告诉我其中的区别。

getX是一个 function,这意味着它是一个可以运行的代码块。 当您要求直接打印 function 时,它会告诉您它可以在 memory 中运行的代码的位置。 通过在末尾添加括号,您是在告诉它实际执行该代码,并且顾名思义,得到 X。

暂无
暂无

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

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