簡體   English   中英

Python -- __str__ 不適用於導入

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM