标题

get() takes 1 positional argument but 2 were given
某某函数设置一个变量,但是你给了两个但是你给了两个参数

这个错误原因有很多:

#第一种:
def fun(x):
      return x
fun(1,2)
#第二种:
class MyProperty:
    def __init__(self,fset=None,fget=None,fdel=None):
        self.fset = fset
        self.fget = fget
        self.fdel = fdel
    def __get__(self, instance, owner):
        return self.fget(instance)
    def __set__(self, instance, value):
        self.fset(instance,value)
    def __delete__(self, instance):
        self.fdel(instance)
class my:
    def __init__(self):
        self._x = None
    def get(self):
        return self._x
    def set(self,value):
        self._x = value
    def delX(self):
        del self._x

    x = MyProperty(get,set,delX)

c = my()
c.x='x_man'
print(c.x)
print(c._x)
'''
D:\Python\python.exe D:/pycharm/code/小甲鱼/11-27/自定义property.py
Traceback (most recent call last):
  File "D:/pycharm/code/小甲鱼/11-27/自定义property.py", line 25, in <module>
    c.x='x_man'
  File "D:/pycharm/code/小甲鱼/11-27/自定义property.py", line 9, in __set__
    self.fset(instance,value)
TypeError: get() takes 1 positional argument but 2 were given

Process finished with exit code 1

'''
#第三种:
就是self参数忘记写了,会导致出现以上问题

问题就出在了 def init(self,fset=None,fget=None,fdel=None):的默认参数位置上,我看了老半天,结果这个参数问题导致了标题问题

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐