Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/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 尝试使用变量调用包方法_Python - Fatal编程技术网

Python 尝试使用变量调用包方法

Python 尝试使用变量调用包方法,python,Python,我试图将该方法存储在一个变量包中,因为它可能会更改,我不想在代码中的多个位置手动更新 import hashlib as hashy foo='hello world' bar='hello world' algo='md5' hfoo=hashy.algo(foo.encode()) hbar=hashy.algo(bar.encode()) 在这种特殊情况下,您可以使用它的名称创建哈希器 import hashlib # don't randomly rename standard

我试图将该方法存储在一个变量包中,因为它可能会更改,我不想在代码中的多个位置手动更新

import hashlib as hashy

foo='hello world'
bar='hello world'

algo='md5'
hfoo=hashy.algo(foo.encode())
hbar=hashy.algo(bar.encode())

在这种特殊情况下,您可以使用它的名称创建哈希器

import hashlib  # don't randomly rename standard libraries
ALGORITHM = 'md5'
h = hashlib.new(ALGORITHM)
h.update('hello world'.encode('utf-8'))
print(h.hexdigest())
如果您认为您可能想要更改正在调用的模块中的哪个函数,可以将其封装在您自己的函数中,这正是您所问问题的一般答案

import hashlib

def hash(s):
    hashlib.md5(s.encode('utf-8'))

print hash('hello world')
您可以在模块上使用按名称检索函数,但这不是通常的方法