【问题标题】:How can I remove a string's parentheses so it can be used in functions?如何删除字符串的括号以便可以在函数中使用?
【发布时间】:2015-08-24 04:19:09
【问题描述】:

如果我调用check('list') 并在我定义的函数中调用check,我想使用if isinstance(value, x): 其中x 是check 的参数,目前'list' 但它可能是任何变量类型,有没有一种简单的方法可以使用list 而不是'list'

【问题讨论】:

    标签: string function python-3.x instance


    【解决方案1】:

    this

    typemap = {
      'list': list
       ...
    }
    
    def check(x):
       ...
      if isinstance(value, typemap[x]):
         ...
    

    但实际上你应该直接传递list

    【讨论】:

    • 呃,没有。您可以像索引任何其他字典一样索引它。正如我所展示的那样。
    猜你喜欢
    • 2022-01-07
    • 2020-04-02
    • 1970-01-01
    • 1970-01-01
    • 2021-01-29
    • 1970-01-01
    • 2023-03-22
    • 2022-01-14
    • 1970-01-01
    相关资源
    最近更新 更多