Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取错误=类型错误:';str';对象不能解释为整数。我正在学习python的方法,我遇到了这个问题_Python_Python 3.x_Magic Methods - Fatal编程技术网

获取错误=类型错误:';str';对象不能解释为整数。我正在学习python的方法,我遇到了这个问题

获取错误=类型错误:';str';对象不能解释为整数。我正在学习python的方法,我遇到了这个问题,python,python-3.x,magic-methods,Python,Python 3.x,Magic Methods,这里有什么问题,请帮忙。我做错了什么 class Name: def __init__(self,first,last): self.first = first self.last = last def __len__(self): return self.first name_1 = Name('FIrst','last') print(len(name_1)) 如果您想获得'First'的长度,\uuuu len\uuuu应该

这里有什么问题,请帮忙。我做错了什么

class Name:

    def __init__(self,first,last):

      self.first = first
      self.last = last


    def __len__(self):
      return self.first

name_1 = Name('FIrst','last')

print(len(name_1))

如果您想获得
'First'
的长度,
\uuuu len\uuuu
应该读取
返回len(self.First)
?噢,谢谢,错误已修复。我们能不能先从uu len uuu()中获取第一个,或者这是不可能的。谢谢我不明白你所说的“不从uu len uu()中获取第一个”。就像在普通方法中一样,你可以只返回self。首先,但你不能在len中执行同样的操作。我知道这是一个愚蠢的问题,但我真的很困惑
\uu len
应该返回一个整数。只要您使用
\uuuu len\uuuu
,则不需要。请使用其他没有此类要求的方法;)