Python return 另一个函数
Python return for another function
我正在使用 PyOpenGL,我想制作小型 RGB 转换器功能。
PyOpenGL 的颜色使用从 0 到 1 的浮点数。
所以 0 0 0 - 黑色
1 1 1 - 白色。
我做这个:
def RGB(Red, Green, Blue):
Red = 1 / 255 * Red
Green = 1 / 255 * Green
Blue = 1 / 255 * Blue
return Red, Green, Blue
我是这样使用他的:
glClearColor(RGB(255, 255, 255), 1)
但我得到错误:
this function takes 4 arguments (2 given)
我不明白如何return多个参数
您的函数 glClearColor
需要 4 个参数,您传递了两个参数:元组和整数。
您想使用 *
运算符解压元组:
def RGB(Red, Green, Blue):
Red = 1 / 255 * Red
Green = 1 / 255 * Green
Blue = 1 / 255 * Blue
return Red, Green, Blue
def glClearColor(a, b, c, d):
print(a, b, c, d)
glClearColor(*RGB(255, 255, 255), 1)
# 1.0 1.0 1.0 1
问题不在于您的 RGB
函数,而在于您如何调用 glClearColor
。
你的 RGB
函数 returns 一个 3 元组,这意味着 glClearColor(RGB(255, 255, 255), 1)
调用 glClearColor
一个元组和 1
(2 个参数,比如错误说)。
您可以使用 *
将 3 元组扩展为 3 个单独的参数:
glClearColor(*RGB(255, 255, 255), 1)
这样 glClearColor
分别调用元组的每个元素 + 1
(总共 4 个参数)。
相当于:
r, g, b = RGB(255, 255, 255)
glClearColor(r, g, b, 1)
你的函数 RGB
returns 元组 (1.0, 1.0, 1.0)
你可以使用 asterisk(*) operator 解压它,像这样 glClearColor(*RGB(255, 255, 255), 1)
.
我正在使用 PyOpenGL,我想制作小型 RGB 转换器功能。 PyOpenGL 的颜色使用从 0 到 1 的浮点数。 所以 0 0 0 - 黑色 1 1 1 - 白色。 我做这个:
def RGB(Red, Green, Blue):
Red = 1 / 255 * Red
Green = 1 / 255 * Green
Blue = 1 / 255 * Blue
return Red, Green, Blue
我是这样使用他的:
glClearColor(RGB(255, 255, 255), 1)
但我得到错误:
this function takes 4 arguments (2 given)
我不明白如何return多个参数
您的函数 glClearColor
需要 4 个参数,您传递了两个参数:元组和整数。
您想使用 *
运算符解压元组:
def RGB(Red, Green, Blue):
Red = 1 / 255 * Red
Green = 1 / 255 * Green
Blue = 1 / 255 * Blue
return Red, Green, Blue
def glClearColor(a, b, c, d):
print(a, b, c, d)
glClearColor(*RGB(255, 255, 255), 1)
# 1.0 1.0 1.0 1
问题不在于您的 RGB
函数,而在于您如何调用 glClearColor
。
你的 RGB
函数 returns 一个 3 元组,这意味着 glClearColor(RGB(255, 255, 255), 1)
调用 glClearColor
一个元组和 1
(2 个参数,比如错误说)。
您可以使用 *
将 3 元组扩展为 3 个单独的参数:
glClearColor(*RGB(255, 255, 255), 1)
这样 glClearColor
分别调用元组的每个元素 + 1
(总共 4 个参数)。
相当于:
r, g, b = RGB(255, 255, 255)
glClearColor(r, g, b, 1)
你的函数 RGB
returns 元组 (1.0, 1.0, 1.0)
你可以使用 asterisk(*) operator 解压它,像这样 glClearColor(*RGB(255, 255, 255), 1)
.