gpt4 book ai didi

python - 无法获取内置类型

转载 作者:行者123 更新时间:2023-12-01 05:26:54 24 4
gpt4 key购买 nike

我正在尝试从 __builtins__ 获取类型

if params.type in ["int", "long", "float"]:
vtype = getattr( __builtins__, params.type )
para = [vtype( para[0] )]

我收到以下错误:

Traceback (most recent call last):
File "message_ajax_handler.py", line 267, in get
vtype = getattr( __builtins__, subset[i] )
AttributeError: 'dict' object has no attribute 'int'

但是当我在命令行中测试时

vtype = getattr( __builtins__, 'int' )

它正在工作。我哪里出错了。

最佳答案

您应该使用__builtin__ module相反:

import __builtin__

vtype = getattr(__builtin__, subset[i])

__builtins__ (带有 s)可以是字典或模块,具体取决于上下文。该对象的存在实际上是一个实现细节。来自 __builtin__ 文档:

CPython implementation detail: Most modules have the name __builtins__ (note the 's') made available as part of their globals. The value of __builtins__ is normally either this module or the value of this modules’s __dict__ attribute. Since this is an implementation detail, it may not be used by alternate implementations of Python.

关于python - 无法获取内置类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21184341/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com