티스토리 뷰

https://stackoverflow.com/a/22156138

 

How can I replace `eval` in my function?

I know very well that it is not good practice using eval in Python. Here is my code: from hashlib import * # added for clarification def get_hash(self): if self.hash_type in ['md5', 'sha1', '

stackoverflow.com

 

eval 기반 구현

from hashlib import * # added for clarification
def get_hash(self):
        if self.hash_type in ['md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512']:
            data = eval(self.hash_type) # <--- how can I replace this?

 

getattr 기반 구현

import hashlib
if self.hash_type in ['md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512']:
     func = getattr(hashlib, self.hash_type)

 

 

YOLOv8 기여하는데 쓰임

 

https://github.com/ultralytics/ultralytics/pull/1273

 

remove eval in parse_model by developer0hye · Pull Request #1273 · ultralytics/ultralytics

I made an effort to minimize the number of calls to the eval() function in parse_model() in order to make the code safer. It is related to #955.

github.com

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함