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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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
Python __getslice\uuuu在函数中如何调用它_Python_Python 3.x - Fatal编程技术网

Python __getslice\uuuu在函数中如何调用它

Python __getslice\uuuu在函数中如何调用它,python,python-3.x,Python,Python 3.x,我有一个类句子,带有一个神奇的方法函数\uuu getslice\uuu 我不明白如何调用该函数 我正在试着做单词的切片。例如,如果字符串是“HELLO WORLD,james”,我在[0:1]处对其进行切片,我希望得到“HELLO” 相反,我得到了这个错误:'method'对象不可下标 这是我的密码: 另外,我在哪里可以了解更多有关魔术方法函数的信息?似乎调用函数比编写函数更困难\uuu getslice\uuu()在Python2.6中被弃用,在Python3中被删除。您应该改为使用\uu

我有一个
类句子
,带有一个神奇的方法函数
\uuu getslice\uuu

我不明白如何调用该函数

我正在试着做单词的切片。例如,如果字符串是
“HELLO WORLD,james”
,我在[0:1]处对其进行切片,我希望得到
“HELLO”

相反,我得到了这个错误:
'method'对象不可下标

这是我的密码:

另外,我在哪里可以了解更多有关魔术方法函数的信息?似乎调用函数比编写函数更困难

\uuu getslice\uuu()
在Python2.6中被弃用,在Python3中被删除。您应该改为使用
\uuu getitem\uuu()
,当使用切片表示法调用时,它将接收一个切片对象

i、 e


有关所有神奇方法的详细信息,请参阅。

当您执行
hippo时。
您实际上是在尝试对方法
hippo进行切片。
。这就是它失败的原因

“方法”对象不可下标
重要注意事项:
\uuuu getslice\uuu
自Python 2.0以来已被弃用,并且在Python 3.x中不可用。引用这位官员的话

自版本2.0以来已弃用:支持切片对象作为方法的参数。(但是,CPython中的内置类型目前仍然实现
\uuu getslice\uuu()
。因此,在实现切片时,必须在派生类中重写它。)

所以你应该用它来切片。引述问题,

例如,如果字符串是“HELLO WORLD,james”,我在[0:1]处对其进行切片,我希望得到“HELLO”

由于您希望通过切片来获取单词,如果传递给
\uuu getitem\uuu
的键是一个对象,则调用
self.getWords()
并对返回的对象进行切片,如下所示

def __getitem__(self, k):
    if isinstance(k, slice):
        return self.getWords()[k]
    return self._string[k]

....
print(hippo[0:1])
['HELLO']
注1:订阅
hippo
对象时,不必显式调用
\uuu getitem\uuuuuuuuuuu
。你可以简单地做

print(hippo[0])
# H
这将使用
k
作为
0
在内部调用
\uuuu getitem\uuuuuu

注2:与前一条相同。您不必显式调用,并且可以使用算术运算符
+
隐式调用,如下所示

print(hippo + "james")
# HELLO WORLD, james
注意3:
\uuuquencateble\uuuuuuu
实现中,您使用的是
has\u key
方法(在字符串上)。因此,在运行时,您的程序将在

AttributeError:'str'对象没有属性'has_key'
也许您想在操作符中使用

def __frequencyTable__(self, word):
    count = 0
    for w in self._string:
        if word in self._string:
            count = count + 1
    return count
PS:我不确定这个
\uu frequencyTable\uuu
方法试图实现什么

print(hippo + "james")
# HELLO WORLD, james
def __frequencyTable__(self, word):
    count = 0
    for w in self._string:
        if word in self._string:
            count = count + 1
    return count