Python 错误 -- "self" 未定义
Python Error -- "self" is not defined
我最近开始学习 Python,我 运行 遇到了 类 和 "self" 未定义的错误。我知道有很多关于这个问题的问题,但我无法根据这些答案找出我的代码的问题。
import random
class Encrypt():
def __init__(self, master):
self.master = master
master.title("Encryption using RSA Algorithms")
x = input("Do you want to Encrypt or Decrypt?")
if x=="Encrypt":
print("Redirecting...")
else:
print("Redirecting...")
choice_1 = "Encrypt"
choice_2 = "Decrypt"
if x==choice_1:
y = input("Do you have a public key?")
if y=="Yes":
print("Redirecting...")
else:
print("Generating a key...")
print(self.publickey_generate()) #Error here that self is not defined
else:
z = input("What is your private key?")
def publickey_generate(self):
for output in range (0,1000):
public = random.randint(0, 1000)
if public <= 500:
public += public
else:
public = public - 1
这是我做的代码,是一个加解密软件。错误(由注释标记)是,
line 23, in Encrypt
print(self.publickey_generate(). NameError: name 'self' is not defined
我不知道为什么会这样。任何意见表示赞赏。
当你在 class 的方法中时,self 指的是对象的实例。
在这里,在第 23 行,您直接在 class 中使用它(我认为这不是一个好方法)。如果您在 publickey_generate()
之前删除 self,它应该可以工作
但老实说,这似乎是一种非常奇怪的对象编程方式...您应该将所有代码放在 class 方法中,并且只在外部使用全局变量(如果需要)。因此,在您的情况下,最简单的方法是将所有代码(publickey_generate()
除外)放在 init 方法中
缩进应如下所示:
import random
class Encrypt():
def __init__(self, master):
self.master = master
master.title("Encryption using RSA Algorithms")
x = input("Do you want to Encrypt or Decrypt?")
if x=="Encrypt":
print("Redirecting...")
else:
print("Redirecting...")
choice_1 = "Encrypt"
choice_2 = "Decrypt"
if x==choice_1:
y = input("Do you have a public key?")
if y=="Yes":
print("Redirecting...")
else:
print("Generating a key...")
print(self.publickey_generate()) #Error here that self is not defined
else:
z = input("What is your private key?")
def publickey_generate(self):
for output in range (0,1000):
public = random.randint(0, 1000)
if public <= 500:
public += public
else:
public = public - 1
我最近开始学习 Python,我 运行 遇到了 类 和 "self" 未定义的错误。我知道有很多关于这个问题的问题,但我无法根据这些答案找出我的代码的问题。
import random
class Encrypt():
def __init__(self, master):
self.master = master
master.title("Encryption using RSA Algorithms")
x = input("Do you want to Encrypt or Decrypt?")
if x=="Encrypt":
print("Redirecting...")
else:
print("Redirecting...")
choice_1 = "Encrypt"
choice_2 = "Decrypt"
if x==choice_1:
y = input("Do you have a public key?")
if y=="Yes":
print("Redirecting...")
else:
print("Generating a key...")
print(self.publickey_generate()) #Error here that self is not defined
else:
z = input("What is your private key?")
def publickey_generate(self):
for output in range (0,1000):
public = random.randint(0, 1000)
if public <= 500:
public += public
else:
public = public - 1
这是我做的代码,是一个加解密软件。错误(由注释标记)是,
line 23, in Encrypt
print(self.publickey_generate(). NameError: name 'self' is not defined
我不知道为什么会这样。任何意见表示赞赏。
当你在 class 的方法中时,self 指的是对象的实例。
在这里,在第 23 行,您直接在 class 中使用它(我认为这不是一个好方法)。如果您在 publickey_generate()
但老实说,这似乎是一种非常奇怪的对象编程方式...您应该将所有代码放在 class 方法中,并且只在外部使用全局变量(如果需要)。因此,在您的情况下,最简单的方法是将所有代码(publickey_generate()
除外)放在 init 方法中
缩进应如下所示:
import random
class Encrypt():
def __init__(self, master):
self.master = master
master.title("Encryption using RSA Algorithms")
x = input("Do you want to Encrypt or Decrypt?")
if x=="Encrypt":
print("Redirecting...")
else:
print("Redirecting...")
choice_1 = "Encrypt"
choice_2 = "Decrypt"
if x==choice_1:
y = input("Do you have a public key?")
if y=="Yes":
print("Redirecting...")
else:
print("Generating a key...")
print(self.publickey_generate()) #Error here that self is not defined
else:
z = input("What is your private key?")
def publickey_generate(self):
for output in range (0,1000):
public = random.randint(0, 1000)
if public <= 500:
public += public
else:
public = public - 1