这个在学习一个类模块的时候创建的两个python的脚本文件
一个文件是包含定义Product类的代码,另一个是则包含将使用这个类的脚本。
class Product:
def __init__(self,description,price,quantity):
self.__description = description
self.__price=price
self.__inventory=quantity
def set__description(self,description):
self.set__description = description
def get__description(self):
return self.__description
description=property(get__description,set__description)
def set__price(self,price):
self.set__price = price
def get__price(self):
return self.__price
price=property(get__price,set__price)
def set__inventory(self,inventory):
self.set__inventory = inventory
def get__inventory(self):
return self.__inventory
price=property(get__inventory,set__inventory)
def buy__Product(self,amount):
self,__inventory=self,__inventory-amount
def __str__(self):
return '{0}-price: ${1:.2f},inventory:{2:d}'.format(self.__description,self,__price,self.__inventory)
然后保存然后再使用from语句从product。py模块文件引用Product类。
from product import Product
prod1=Product('carrot',1.25,10)
print(prod1)
print ('Buying 4 carrots...')
prod1.buy__Product(4)
print(prod1)
print('Changing the price to $1.50')
prod1.price=1.50
print (prod1)
然后就很华丽的报错了
runfile('D:/Holleword-python/实验库/dir(math).py', wdir='D:/Holleword-python/实验库')
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\IPython\core\interactiveshell.py", line 2894, in run_code
self.showtraceback()
File "C:\Python27\lib\site-packages\IPython\core\interactiveshell.py", line 1756, in showtraceback
value, tb, tb_offset=tb_offset)
File "C:\Python27\lib\site-packages\IPython\core\ultratb.py", line 1190, in structured_traceback
self, etype, value, tb, tb_offset, context)
File "C:\Python27\lib\site-packages\IPython\core\ultratb.py", line 1107, in structured_traceback
self, etype, value, elist, tb_offset, context
File "C:\Python27\lib\site-packages\IPython\core\ultratb.py", line 501, in structured_traceback
out_list.extend(self._format_list(elist))
File "C:\Python27\lib\site-packages\IPython\core\ultratb.py", line 548, in _format_list
Colors.Normal)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 20: ordinal not in range(128)
然后就很忧伤,等我把这个问题解决了再来更新吧
:(
————————————————————————
哈哈哈哈哈,只要把他们放在同一个文件夹就可以了