Python 中的命名参数和默认参数类参数

发布于 2024-08-06 02:48:21 字数 326 浏览 3 评论 0原文

在 PHP 中,我必须按照与构造函数中参数相同的顺序传递参数。

现在,以 Python

listbox = Listbox(root, yscrollcommand=scrollbar.set)

为例。

如果我将 yscrollcommand=scrollbar.set 作为第三个参数传递,并且 yscrollcommand 是构造函数中的第二个参数,我是否仍然可以从列表框构造函数内的 yscrollcommand 访问scrollbar.set?

我问这个问题是因为即使参数的顺序不同,但它们都有一个名称,带有等号。

In PHP, I have to pass the arguments in the same order as the arguments are in the constructor.

Now, in Python, take

listbox = Listbox(root, yscrollcommand=scrollbar.set)

for example.

If I had passed yscrollcommand=scrollbar.set as the third argument and yscrollcommand was the second argument in the constructor, would I still be able to access scrollbar.set from yscrollcommand inside the listbox constructor?

I ask this because even though the arguments are not in the same order, with an equal sign they have a name.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半葬歌 2024-08-13 02:48:21

命名参数不必按顺序排列。

class xyz:
    def __init__ (self, a='1', b='2'):
        print a,b

xyz(b=3,a=4)
xyz(a=5,b=6)

>>4 3
>>5 6

Named arguments don't have to be in order.

class xyz:
    def __init__ (self, a='1', b='2'):
        print a,b

xyz(b=3,a=4)
xyz(a=5,b=6)

>>4 3
>>5 6
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文