![](/img/trans.png)
[英]Does python `str()` function call `__str__()` function of a class?
[英]Python -- __str__ does not work on import
我是 Python 新手,但我遇到了与此线程类似的问题
我目前正在运行:
蟒蛇 3.6.7
海湾合作委员会 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)
我感谢任何输入
编辑:我这里的代码是我用来重现错误的所有代码。 我的猜测是这可能是我在 Ubuntu 中使用 Python3 的设置中的错误
对我来说工作正常。
$ 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.