Python的 - __str__不会对进口工作

问题描述 投票:1回答:1

我是新来的Python,但我一直有类似这样的thread这个问题

我目前正在运行:

Python的3.6.7 GCC 8.2.0 没有IDE只是普通的* .py文件

这里是我的类:

class Point:                                                                                                                                  
    """ Point class represents and manipulates x,y coordinates """

    def __init__(self, x=0, y=0):                                                   
        """ Create a new point at the origin """                                    
        self.x = x                                                                  
        self.y = y                                                                  

    def __str__(self):                                                              
        return "({0}, {1})".format(self.x, self.y)   

p = Point()
print(p)  

我很好奇,为什么__str__工作在同一个文件,但回报:

<point.Point object at 0x7eff98cc4c18> 

之后我导入到another.py文件

我的导入文件是这样的:

from point import Point
p = Point()
print(p)

我明白任何输入

编辑:我这里的代码是我用过重现bug的所有代码。我的猜测是,这可能是我在Ubuntu安装与Python3错误

python
1个回答
1
投票

工程确定为我。

$ python --version
Python 3.6.5
$ python Point.py 
(0, 0)
$ python
Python 3.6.5 (default, Nov 18 2018, 02:06:39) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from Point import Point
(0, 0)
>>> p = Point()
>>> p
<Point.Point object at 0x7fe04d5470f0>
>>> print(p)
(0, 0)
>>> 

© www.soinside.com 2019 - 2024. All rights reserved.