【问题标题】:About OOP self parameter unfilled关于OOP自参数未填写
【发布时间】:2021-01-12 03:35:19
【问题描述】:

我开发了一个加密数据的简单程序,但我不知道这有什么问题。 它显示 self 参数未填充。我在哪里犯了错误? 这是我的代码。

from cryptography.fernet import Fernet
 
 
class Encryption:
    def __init__(self):
        self.key = open("secret.key", "rb").read()
 
    def encrypting_text(self, data):
        data = data.encode()
        key = self.key
        f = Fernet(key)
        encrypted_message = f.encrypt(data).decode()
        print(encrypted_message)
 
 
Encryption.encrypting_text(data='hello world!')

【问题讨论】:

    标签: python-3.x oop


    【解决方案1】:

    您需要使用类的instance 来调用其方法,而不是class 本身(因为encrypting_text 未声明为classmethod)。 self 指的是当前类的实例:

    # Encryption() is the 'instance' of class 'Encryption'
    Encryption().encrypting_text(data='hello world!')
    

    【讨论】:

    • @XYZGODGAMING 如果问题得到解决,请将答案标记为已接受(点击灰色✔️)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    相关资源
    最近更新 更多