第一个照着书写了一段代码,但就是两段代码无法使用,成独立的了:(

    这个在学习一个类模块的时候创建的两个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)

    然后就很忧伤,等我把这个问题解决了再来更新吧
    :(

————————————————————————
哈哈哈哈哈,只要把他们放在同一个文件夹就可以了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值