元组的reference前加个星号是什么意思?
1 2 |
|
回复内容:
我不会用PyGObject, 纯讨论星号参数这个语法.类似问题(也许对你有帮助):
定义函数def func(**kwargs):print kwargs调用函数的时候 一定要func(a=1,b=2,c=3)这样吗?dict_t={'a':1,'b':2,'c':3}不能直接传字典吗?例如func(dict_t)。有什么好方法吗?
Python 参数知识(变量前加星号的意义)
咱们先搞定星号参数的含义以及2个例子:
在参数名之前使用1个星号,就是让函数接受任意多的位置参数。
2个星号就是接受任意多的关键字参数。
位置参数的举例:
假设你有这么个需求:把某函数接收到的除了第一个参数之外的所有参数输出。

关键字参数的举例:
不管你传入多少个关键字参数, 我都能在kw里找到.

接着来搞定这个类似于你在问题中的补充资料中的例子:
下面这个例子接受任意多的位置参数, 只输出第一个

咱们定义一个新元组,待会扔给mean函数。

2种用法的区别:


上面这个例子等价于:

================== 2013-2-26更新 ================

s 是元组, 所以值不可修改。
我这里企图把位置参数里的第1个的值修改成5,然后输出。
使用元组看看:


我们换成列表看看:

用字符串看看:

我们这里拆了列表,元组,字符串变成位置参数再传进去。
可以看到不论如何。s 都是元组。
存储位置参数的这个 s 变量的类型并不取决于传入的参数的类型。
================= 再随便多写一些例子 =================:
普通函数:

文艺函数:

mean 遍历输出所有位置参数。s 是元组,别忘了。
咱们拆一下 列表 和 字符串。

拆元组:

拆字典:


拆字典它只会把[键]传进去,你是取不到值的。

====== 要接受拆字典的值。函数形参用2个星号就行了。=======
这里的意思是: 把所有的关键字参数都扔给s (不包括位置参数)


c前面的2个星号只是代表把 c 拆成关键字参数形式。
用的时候我们把字典拆成了关键字参数。
而函数接收的也是关键字参数。所以这个不会报错。
等价于这么用:

如果你用函数的时候只给了1个星号:

或是不给星号:

为什么??
放1个星号:
字典前面放个1个星号的确是拆开的意思。
1个星号拆字典会只是传入[键]。之前的一个例子证明过了。
问题是我们的函数mean它只接受关键字参数(回顾一下我们前面对mean的定义):

不放星号:
不放星号就代表你把那个字典直接扔进去了。
问题是函数形参**s只接受关键字参数。
s 里面还是啥也没有, 报错!
(所有的举例所使用到的 函数名 和 参数名 都是随便取的, 没有什么特殊意义.)
感觉自己举例的还不是很好,如果哪里把你弄迷糊了。
欢迎在评论区讨论. :)
扩展阅读环节:
StackOverflow 上解释星号语法的问题:
syntax - Python: Once and for all. What does the Star operator mean in Python?
引用传值的问题:
Python: How do I pass a variable by reference? 表示把这个参数展开传进去。等价于:
1 |
|
func(*sequence) Pass all objects in sequence as individual positional arguments
seq = [1,2,3]
func(*seq) -> func(1, 2, 3)
func(**dict) Pass all key/value pairs in dict as individual keyword arguments
dict = {'a' = 1, 'b' = 2}
func(*dict) -> func(a = 1, b = 2)
函数定义的
def func(*name) Matches and collects remaining positional arguments in a tuple
func(1, 2, 3) -> name = [1, 2, 3]
def func(**name) Matches and collects remaining keyword arguments in a dictionary
func(a = 1, b= 2) -> dict = {'a' = 1, 'b' = 2}
可参考
1. 定义函数时
一般情况下,函数的参数接受指定个数的参数,比如
1 2 |
|
... print args, kwargs
>>>a={"a":1, "b":2}
>>> test(a)
({'a': 1, 'b': 2},) {}
>>> test(*a) #这里等同于test('a', 'b')
('a', 'b') {}
>>> test(**a) #这里等同于test(a=1, b=2)
() {'a': 1, 'b': 2}
一个星号就是把一个序列拆解传入,如果变量本身是字典,会退化成key的序列。
两个星号就是把key-value拆解传入。
姑且就当是语法糖吧。 搬运工:1.2 解压可迭代对象赋值给多个变量
这个解压非常dedicate,具有很多优雅的技巧。
总结如下:
(1)
完全不需要tem中间变量的交换:
a,b =b,a
(2)
有时候,你可能只想解压一部分,丢弃其他的值。对于这种情况Python并没有提供特殊的语法。 但是你可以使用任意变量名去占位,到时候丢掉这些变量就行了。
1 2 |
|

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Fujifilm fans were recently very excited at the prospect of the X-T50, since it presented a relaunch of the budget-oriented Fujifilm X-T30 II that had become quite popular in the sub-$1,000 APS-C category. Unfortunately, as the Fujifilm X-T50's launc

The Dasung Paperlike Color with a screen diagonal of 12 inches is now available. The model, which was announceda few months ago,offers a very high resolution of 2,560 x 1,600 and therefore a high level of sharpness, particularly in view of its relati

1. Introduction to the screen command Screen is a full-screen window manager that multiplexes physical terminals between multiple processes (usually interactive shells). Each virtual terminal provides the functionality of a DECVT100 terminal, as well as several control functions of ANSIX3. 64 (ISO6429) and ISO2022 standards (e.g. insert/delete lines and support for multiple character sets). Each virtual terminal has a scrollback history buffer and a copy-paste mechanism that allows the user to move text areas between windows. When screen is called, it creates a window containing the shell (or the specified command) and then gets out of your way so that you can use the program normally. Then at any time you can create a

SincethelaunchoftheGooglePixel8(approx.513eurosatAmazon,limited-timedeal),therehavebeenanincreasingnumberofreportsfromuserscomplainingaboutaverticallineonthedisplay.Thiscanmanifestitselfindifferentways-thecolorand

Detailed explanation of CSS dimension properties: height and width In front-end development, CSS is a powerful style definition language. Among them, height and width are the two most basic dimension attributes, used to define the height and width of the element. This article will analyze these two properties in detail and provide specific code examples. 1. Height attribute The height attribute is used to define the height of an element. You can use pixel, percentage or

The N1S Pro is anotherprojector from JMGO, that is also available now. Specifically, it is offered for a price of US$1999 on Amazon. The 4K projector is sold directly by JMGO, with Amazon handling the shipping itself. The model is currently in stock

We often encounter this problem. We use SSH to log in to a remote Linux server and run some long-term tasks. However, the task fails midway due to network instability. How to prevent the command from being interfered by closing the terminal window locally/disconnecting the network after the command is submitted? Screen is a free software developed by the GNU Project for command line terminal switching. Users can use this software to connect to multiple local or remote command line sessions at the same time and switch between them freely. Screen can be thought of as the command line interface version of a window manager. It provides a unified interface and corresponding functions for managing multiple sessions. Method: 1. Check if there is a screen installation package on the YUM source #yumlistscreen

jQuery is a JavaScript library widely used in web development. It provides developers with many convenient methods to manipulate and process web page elements. In actual development, we often need to operate the attributes of web page elements. One of the common requirements is to remove the height attribute of the element. In this article, we will introduce how to use jQuery to quickly remove the height attribute of an element and provide specific code examples. To remove the height attribute of an element, you can use jQu
