[問題] python3 class的載入
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
推
02/26 20:29, , 1F
02/26 20:29, 1F
Python 近期熱門文章
PTT數位生活區 即時熱門文章