Lexical cast from string to type

前端 未结 4 1000
粉色の甜心
粉色の甜心 2020-12-01 09:26

Recently, I was trying to store and read information from files in Python, and came across a slight problem: I wanted to read type information from text files. Type casting

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 10:03

    Perhaps this is what you want, it looks into builtin types only:

    def gettype(name):
        t = getattr(__builtins__, name)
        if isinstance(t, type):
            return t
        raise ValueError(name)
    

提交回复
热议问题