作者zeldo (Zeldo)
看板Python
标题[问题] python3 class的载入
时间Fri Feb 26 08:51:25 2010
python3 class的载入
最近随着线上的练习,练习到class的部分。
跟着范例程式将之前所练习的程式import入新的程式中。
不过每当在执行的时候,都会跑出
UnicodeDecodeError: 'utf8' codec can't decode byte 0xae in position 31: unexpected code byte
的错误讯息。
我是先开文字档写好後,以UTF-8的编码储存的。
如果说:
#-*- coding: UTF-8 -*-
from point import Point
a = Point(1,1)
b = Point(2,2)
print(a,"+",b,"=",a+b)
print()
input("请点<Enter>来结束视窗。")
这样的话执行就会跑出上头的问题。
可是如果将"point"这个程式直接打在上头,就可以执行。
如这样:
#-*- coding: UTF-8 -*-
class Point(object):
def __init__(self, x=0, y=0):
self.x=x
self.y=y
def __str__(self):
return "(" + str(self.x) + "," + str(self.y) + ")"
def __add__(self,other):
return Point(self.x + other.x , self.y + other.y)
def __sub__(self,other):
return Point(self.x - other.x , self.y - other.y)
def __mul__(self,other):
return self.x * other.x + self.y * other.y
def __rmul__(self,other):
return Point(other * self.x, other * self.y)
a = Point(1,1)
b = Point(2,2)
print(a,"+",b,"=",a+b)
print()
input("请点<Enter>来结束视窗。")
这是执行结果:
(1,1) + (2,2) = (3,3)
请点<Enter>来结束视窗。
想请教一下,要如何改善这个问题。
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 61.231.55.72
1F:推 ibmibmibm:你的档案没有存成UTF8编码吧 02/26 20:29