简体   繁体   English

Python -- __str__ 不适用于导入

[英]Python -- __str__ does not work on import

I'm new to Python but I've been having this problem similar to this thread我是 Python 新手,但我遇到了与此线程类似的问题

I'm currently running:我目前正在运行:

Python 3.6.7蟒蛇 3.6.7
GCC 8.2.0海湾合作委员会 8.2.0
No IDE just plain *.py files没有 IDE 只是普通的 *.py 文件

Here's my class:这是我的课:

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)  

I was curious why the __str__ works on the same file but returns:我很好奇为什么__str__在同一个文件上工作但返回:

<point.Point object at 0x7eff98cc4c18> 

after I imported to another.py file在我导入到 another.py 文件之后

My import file is this:我的导入文件是这样的:

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

I appreciate any input我感谢任何输入

Edit: The code I have here is all the code I've used to reproduce the bug.编辑:我这里的代码是我用来重现错误的所有代码。 My guess is that this might be an error in my setup with Python3 in Ubuntu我的猜测是这可能是我在 Ubuntu 中使用 Python3 的设置中的错误

Works OK for me.对我来说工作正常。

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

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

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