Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to call dynamicly string operations in Python?

//func_to_exec parameter is coming from database dynamically.
func_to_exec='split("\|")[0].split(",")[1]'

pl='mancity,manunited,arsenal|2|3|4|5'

is there anyway to call

pl.func_to_exec

I saw exec and eval functions are only for integers. I cant find any solution for strings.
Thx for suggestions.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use the exec function for that:

pl = 'mancity,manunited,arsenal|2|3|4|5'
func_to_exec = 'split("\|")[0].split(",")[1]'

exec(f'result = pl.{func_to_exec}')
print(result)  # Output: 'manunited'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading