Kivy python*args

2024-06-16 14:18:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚学过kivy,我不明白本教程中调用者的一些参数:

http://pythonmobile.blogspot.com/2014/06/18-kivy-app-bezier-weight.html

我知道*arg是传递给调用者的对象列表。但在这个函数中:

class Ex18(BoxLayout):
    def newWeights(self, *args):
        t = args[1]
        self.ids['w0'].value = (1-t)**3
        self.ids['w1'].value = 3*t*(1-t)**2
        self.ids['w2'].value = 3*t**2*(1-t)
        self.ids['w3'].value = t**3

args[1]是什么意思?在

这位KV语言的来电者:

^{pr2}$

这个调用者的*参数是什么。哪些参数被传递给这个,它不是显式的。希望你能理解我的意思,我的英语不好。非常感谢!在


Tags: selfcomappidshttp参数valueargs
1条回答
网友
1楼 · 发布于 2024-06-16 14:18:51

*args允许向函数传递任意数量的参数,它是传递给函数的参数元组。在

def foo(*args):
    for i in args:
        print i
    print type(args)

foo(1, 2, 3,'a')

output:-
>>> 
1
2
3
a
<type 'tuple'>

当您实际上不知道在函数调用中传递了多少个参数时,就使用它。在

^{pr2}$

显示args的类型

所以:-

arg[0] = first element of tuple
arg[1] = second element of tuple
.
.
and so on

虽然不需要像知道传递给函数调用的nombers参数一样

def foo(a, b, c, d, e):
    print a, b, c, d, e

foo(1, 2, 'this is','string', [1, 2 ,3, 4])


>>> 
1 2 this is string [1, 2, 3, 4]

相关问题 更多 >