TypeError:_transform()接受2个位置参数,但给出了3个 - python

我了解了CaesarCipher:

In [90]: !cat caesar_cipher.py                                                                                                                        
class CaesarCipher:
    """Construct Caesar cipher using given integer shift for rotation."""
    def __init__(self, shift):
        encoder = [None] * 26
        decoder = [None] * 26
        for k in range(26):
            encoder[k] = chr((k + shift)%26 + ord('A'))
            decoder[k] = chr((k - shift)%26 + ord('A'))  #find the number of Letters
        self.encoder = "".join(encoder)
        self.decoder = "".join(decoder)

    def encrypt(self, message):
        print(self.encoder)
        return self._transform(message, self.encoder)

    def decrypt(self, message):
        return self._transform(message, self.decoder)

    def _transform(original, code):
        msg = list(original)
        for k in range(len(msg)):
            j = ord(msg[k]) - ord('A')
            msg[k] = code[j]
        return "".join(msg)

if __name__ == "__main__":
    cipher = CaesarCipher(3)
    message = "THE EAGLE IS IN PLAY; MEET AT JOE'S."
    coded = cipher.encrypt(message)
    print("Secret: ", coded)
    answer = cipher.decrypt(coded)
    print("Message: ", answer)

它在_trasform上报告错误

In [91]: !python caesar_cipher.py                                                                                                                     
DEFGHIJKLMNOPQRSTUVWXYZABC
Traceback (most recent call last):
  File "caesar_cipher.py", line 29, in <module>
    coded = cipher.encrypt(message)
  File "caesar_cipher.py", line 14, in encrypt
    return self._transform(message, self.encoder)
TypeError: _transform() takes 2 positional arguments but 3 were given

“ _transform()需要2个位置参数”,我确实给出了2个参数

为什么给出报告3?

python大神给出的解决方案

您需要将其定义为

def _transform(self, original, code)

子条件的python条件覆盖 - python

我试图找到一个python代码覆盖率工具,该工具可以衡量语句中是否包含子表达式:例如,我想看看下面的示例是否涵盖了condition1 / condition2 / condtion3?if condition1 or condition2 or condition3: x = true_value python大神给出的解决方案 对此的唯一合理答案是:当前…

USB设备发行 - python

我目前正在使用PyUSB。由于我不熟悉USB,所以我不知道如何执行以下操作。我已经从Python PyUSB成功连接到我的USB设备硬件。在代码中,我需要重置USB设备硬件。通过向硬件发送命令来完成。现在,在硬件重置后,我想从Python PyUSB释放当前的USB设备。然后,我想在重置后将其重新连接到USB设备硬件。请让我知道,如何释放USB设备连接和接口…

Python-熊猫描述了抛出错误:无法散列的类型“ dict” - python

更新:我正在使用“ Socrata开源API”中的一些示例代码。我在代码中注意到以下注释:# First 2000 results, returned as JSON from API / converted to Python # list of dictionaries by sodapy. 我不熟悉JSON。我已经下载了一个数据集,并创建了一个包含大量…

在Pytorch中重复张量的特定列 - python

我有一个大小为X的pytorch张量m x n和一个长度为num_repeats的非负整数n列表(假定sum(num_repeats)> 0)。在forward()方法中,我想创建一个大小为X_dup的张量m x sum(num_repeats),其中i的列X重复num_repeats[i]次。张量X_dup将在forward()方法的下游使用,因此需…

在屏幕上打印错误,但继续执行代码 - python

我有一些代码可以通过一系列URL进行迭代。如果由于其中一个URL不包含有效的JSON正文而导致我的代码中出现错误,我希望将生成的错误打印到屏幕上,然后将代码移至下一个迭代。我的代码的简单版本是:for a in myurls: try: #mycode except Exception as exc: print traceback.format_exc()…