鱼C论坛

 找回密码
 立即注册
查看: 1324|回复: 3

[已解决]鱼友们,这里为什么会出错呢?

[复制链接]
发表于 2020-7-31 21:56:56 | 显示全部楼层 |阅读模式

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

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

x
这道题是想统计传入的参数有多少个,然后我就用两种做法来做,代码如下:

1.
class Sum:
    def __init__(self,*arg):
        self.len = len(arg)
        if self.len == 0 :
            print("并没有传入参数")
        else:
            print("传入了%d个参数,分别是:"%self.len,end="")
            for i in arg:
                print("%d "%i,end="")

2.
class Sum:
    total = 0
    def __init__(self,*argv):
        self.List = list()
        for i in argv:
            self.List.append(i)
        if Sum.total==0:
            print("并没有传入参数")
        else:
            print("传入了%d个参数,分别是:"%Sum.total,end="")
            for i in argv:
                print("%d "%i,end="")
    def __getattribute__(self,total):
        Sum.total += 1

但是第二种会产生错误,为什么呢?
AttributeError: 'NoneType' object has no attribute 'append'
最佳答案
2020-8-1 08:47:03



为什么第二种会产生错误呢?


因为你 __getattribute__ 函数是用来获取属性,而你将这个函数没有设置怎么获取类中的属性,而你self.List.append(i) 的时候是会自动调用 __getattribute__函数来获取 self.List 这个属性

你重写了 __getattribute__ 而且没有设置返回值,导致你访问类中的属性时候返回的值为 None

而 None 没有列表的这种函数方法 append ,导致报错,所以你只需要在你 __getattribute__ 调用下父类方法即可,代码如下:



  1. class Sum:
  2.     total = 0
  3.     def __init__(self,*argv):
  4.         self.List = list()
  5.         for i in argv:
  6.             self.List.append(i)
  7.         if Sum.total==0:
  8.             print("并没有传入参数")
  9.         else:
  10.             print("传入了%d个参数,分别是:"%Sum.total,end="")
  11.             for i in argv:
  12.                 print("%d "%i,end="")
  13.     def __getattribute__(self,total):
  14.         Sum.total += 1
  15.         return super().__getattribute__(total)  # 这里调用父类的 __getattribute__ 方法即可正常运行
  16. a = Sum(1,2,3,4,5)
复制代码


输出结果:
  1. 传入了5个参数,分别是:1 2 3 4 5
复制代码




小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-7-31 22:15:30 | 显示全部楼层
  1. class Sum:
  2.     def __init__(self,*argv):
  3.         self.total = 0
  4.         self.List = list()

  5.         for i in argv:
  6.             self.List.append(i)
  7.             self.total += 1

  8.         if self.total==0:
  9.             print("并没有传入参数")
  10.         else:
  11.             print("传入了%d个参数,分别是:"%self.total,end="")
  12.             for i in argv:
  13.                 print("%d "%i,end="")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-1 02:51:27 | 显示全部楼层
本帖最后由 全能小乌贼 于 2020-8-11 22:33 编辑

个人认为这个是因为你的__getattribute__函数中没有返回值,所以程序在初始化运行self.List = list()时进入到调用__getattribute__函数执行完成Sum.total += 1后返回了一个空值,从而使得实际上self.List = list()的列表定义变成了self.List = None的定义,最后就造成了AttributeError: 'NoneType' object has no attribute 'append'的错误。
解决方法:给你的__getattribute__函数末尾加上return object.__getattribute__(self, total),亲测有效!!!
最后,have a nice day! 一起加油学习啊!
完整代码如下:

  1. class Sum(object):
  2.     total = 0
  3.     def __init__(self, *argv):
  4.         self.List = []
  5.         for i in argv:
  6.             self.List.append(i)
  7.         if Sum.total == 0:
  8.             print("并没有传入参数")
  9.         else:
  10.             print("传入了%d个参数,分别是:" % Sum.total, end="")
  11.         for i in argv:
  12.             print("%d " % i, end="")

  13.     def __getattribute__(self, total):
  14.             Sum.total += 1
  15.             return object.__getattribute__(self, total)


  16. main = Sum(111)
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-1 08:47:03 | 显示全部楼层    本楼为最佳答案   



为什么第二种会产生错误呢?


因为你 __getattribute__ 函数是用来获取属性,而你将这个函数没有设置怎么获取类中的属性,而你self.List.append(i) 的时候是会自动调用 __getattribute__函数来获取 self.List 这个属性

你重写了 __getattribute__ 而且没有设置返回值,导致你访问类中的属性时候返回的值为 None

而 None 没有列表的这种函数方法 append ,导致报错,所以你只需要在你 __getattribute__ 调用下父类方法即可,代码如下:



  1. class Sum:
  2.     total = 0
  3.     def __init__(self,*argv):
  4.         self.List = list()
  5.         for i in argv:
  6.             self.List.append(i)
  7.         if Sum.total==0:
  8.             print("并没有传入参数")
  9.         else:
  10.             print("传入了%d个参数,分别是:"%Sum.total,end="")
  11.             for i in argv:
  12.                 print("%d "%i,end="")
  13.     def __getattribute__(self,total):
  14.         Sum.total += 1
  15.         return super().__getattribute__(total)  # 这里调用父类的 __getattribute__ 方法即可正常运行
  16. a = Sum(1,2,3,4,5)
复制代码


输出结果:
  1. 传入了5个参数,分别是:1 2 3 4 5
复制代码




小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-24 12:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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