Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Substitute class method with a string in Python

Lets say that I want to do this

hashlibAlgo = "md5"
Hash= hashlib.**"hashlibAlgo"**("blah blah blah").hexdigest()

How can I do that. If I substitute the name of a method with a string it obviously does not work. How can I make it work? In powershell is easy, but I cannot figure it in Python.

like image 731
Juan C. Avatar asked Jul 13 '26 10:07

Juan C.


1 Answers

You can get function to execute with getattr:

>>> import hashlib
>>> hashlibAlgo = "md5"
>>> getattr(hashlib, hashlibAlgo)("blah blah blah").hexdigest()
'55e562bfee2bde4f9e71b8885eb5e303'
like image 107
Alexey Kachayev Avatar answered Jul 14 '26 23:07

Alexey Kachayev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!