鱼C论坛

 找回密码
 立即注册
查看: 1872|回复: 13

[已解决]小白Python求助

[复制链接]
发表于 2017-4-25 21:30:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. class Rectangle:
  2.     def __init__(self, width = 0, height = 0):
  3.         self.width = width
  4.         self.height = height

  5.     def __setattr__(self, name, value):
  6.         if name == 'square':
  7.             self.width = value
  8.             self.height = value
  9.         else:
  10.             super( ).__setattr__(name, value)

  11.     def getArea(self):
  12.         return self.width*self.height
复制代码


Windows系统下,Python2版本,运行后老是出现如下错误,求帮助解答。(小白一只)
  1. r = Rectangle(2,3)

  2. Traceback (most recent call last):
  3.   File "<pyshell#26>", line 1, in <module>
  4.     r = Rectangle(2,3)
  5.   File "E:\python programm\try.py", line 3, in __init__
  6.     self.width = width
  7.   File "E:\python programm\try.py", line 11, in __setattr__
  8.     super( ).__setattr__(name, value)
  9. TypeError: super() takes at least 1 argument (0 given)
复制代码


不知道原因,明明是跟着小甲鱼一起敲的代码!!!
但是换成下列方法就是正确的
  1. class Rectangle:
  2.     def __init__(self, width = 0, height = 0):
  3.         self.width = width
  4.         self.height = height

  5.     def __setattr__(self, name, value):
  6.         if name == 'square':
  7.             self.width = value
  8.             self.height = value
  9.         else:
  10.             self.__dict__[name] = value

  11.     def getArea(self):
  12.         return self.width*self.height
复制代码

然后可以得到正确结果。
  1. >>> r = Rectangle(2,3)
  2. >>> r.square = 10
  3. >>> r.width
  4. 10
  5. >>> r.getArea()
  6. 100
  7. >>>
复制代码
最佳答案
2017-4-25 21:36:51
sunshine似我 发表于 2017-4-25 21:36
好吧,看来也只能这样。之前还因为字体的原因也一直没能解决,中文运行的结果是乱码,修改了字体也不行, ...
  1. class Rectangle(object):
  2.     def __init__(self, width = 0, height = 0):
  3.         self.width = width
  4.         self.height = height

  5.     def __setattr__(self, name, value):
  6.         if name == 'square':
  7.             self.width = value
  8.             self.height = value
  9.         else:
  10.             super( ).__setattr__(name, value)

  11.     def getArea(self):
  12.         return self.width*self.height
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-4-25 21:32:29 | 显示全部楼层
我试了,没错(我用的3)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-25 21:33:51 | 显示全部楼层
你的错误是:Python2里class时必须有参数
  1. class Rectangle(object):
  2.     def __init__(self, width = 0, height = 0):
  3.         self.width = width
  4.         self.height = height

  5.     def __setattr__(self, name, value):
  6.         if name == 'square':
  7.             self.width = value
  8.             self.height = value
  9.         else:
  10.             super( ).__setattr__(name, value)

  11.     def getArea(self):
  12.         return self.width*self.height
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-25 21:33:54 | 显示全部楼层
新手·ing 发表于 2017-4-25 21:32
我试了,没错(我用的3)

难道是因为版本的问题???
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-25 21:34:48 | 显示全部楼层
sunshine似我 发表于 2017-4-25 21:33
难道是因为版本的问题???

是的(版本差异)
3里面可以没有参数
来3吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-25 21:36:15 | 显示全部楼层
新手·ing 发表于 2017-4-25 21:34
是的(版本差异)
3里面可以没有参数
来3吧

好吧,看来也只能这样。之前还因为字体的原因也一直没能解决,中文运行的结果是乱码,修改了字体也不行,是不是2的问题还很多啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-25 21:36:40 | 显示全部楼层
sunshine似我 发表于 2017-4-25 21:36
好吧,看来也只能这样。之前还因为字体的原因也一直没能解决,中文运行的结果是乱码,修改了字体也不行, ...

我给你解决方案了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-25 21:36:51 | 显示全部楼层    本楼为最佳答案   
sunshine似我 发表于 2017-4-25 21:36
好吧,看来也只能这样。之前还因为字体的原因也一直没能解决,中文运行的结果是乱码,修改了字体也不行, ...
  1. class Rectangle(object):
  2.     def __init__(self, width = 0, height = 0):
  3.         self.width = width
  4.         self.height = height

  5.     def __setattr__(self, name, value):
  6.         if name == 'square':
  7.             self.width = value
  8.             self.height = value
  9.         else:
  10.             super( ).__setattr__(name, value)

  11.     def getArea(self):
  12.         return self.width*self.height
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 0 反对 1

使用道具 举报

 楼主| 发表于 2017-4-25 21:38:08 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-25 21:38:43 | 显示全部楼层

没事
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-25 21:50:50 | 显示全部楼层

不过我试了,好像不是这个原因,照样报错
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-25 21:54:04 | 显示全部楼层
sunshine似我 发表于 2017-4-25 21:50
不过我试了,好像不是这个原因,照样报错

好尴尬
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-25 21:55:16 | 显示全部楼层

你得给super一个参数
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-25 22:00:26 | 显示全部楼层
新手·ing 发表于 2017-4-25 21:55
你得给super一个参数

求指导怎么改
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-12 14:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表