cuốn sách gpt4 ai đã làm

python - Làm cách nào để lấy số lượng tham số của hàm dựng sẵn trong Python?

In lại Tác giả: Vũ trụ không gian Thời gian cập nhật: 2023-11-03 15:28:26 hai mươi bốn 4
mua khóa gpt4 Nike

我需要以编程方式获取函数所需的参数数量。对于在模块中声明的函数,这是微不足道的:

myfunc.func_code.co_argcount

但是内置函数没有func_code 属性。还有另一种方法吗?否则我无法使用内置插件,必须在我的代码中重新编写它们。

[补充]感谢您的回复,希望它们有用。我改用了 Pypy。

câu trả lời hay nhất

看看下面从 đây 复制的函数.这可能是你能做的最好的。请注意有关 inspect.getargspec 的注释。

def describe_builtin(obj):
""" Describe a builtin function """

wi('+Built-in Function: %s' % obj.__name__)
# Built-in functions cannot be inspected by
# inspect.getargspec. We have to try and parse
# the __doc__ attribute of the function.
docstr = obj.__doc__
args = ''

if docstr:
items = docstr.split('\n')
if items:
func_descr = items[0]
s = func_descr.replace(obj.__name__,'')
idx1 = s.find('(')
idx2 = s.find(')',idx1)
if idx1 != -1 and idx2 != -1 and (idx2>idx1+1):
args = s[idx1+1:idx2]
wi('\t-Method Arguments:', args)

if args=='':
wi('\t-Method Arguments: None')

in

关于python - 如何获取 Python 中内置函数的参数个数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3276635/

hai mươi bốn 4 0
không gian vũ trụ
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress