*params、**params: Collecting Parameters and Distribute Parameters

################################   *   ###############################

The star in front of the parameter puts all the values into the same tuple. 

def print_params_2(title, *params):

    print title

    print params

 

>>> print_params_2('Params:', 1, 2, 3)

Params:

(1, 2, 3)

################################   **   ##############################

 ** “gathering” operator for keyword arguments.

def print_params_3(**params):

    print params

 

>>> print_params_3(x=1, y=2, z=3)

{'z': 3, 'x': 1, 'y': 2}

 

################################ more practice ######################

def print_params_4(x, y, z=3, *pospar, **keypar):

    print x, y, z

    print pospar

    print keypar

 

 

>>> print_params_4(1, 2, 3, 5, 6, 7, foo=1, bar=2)

1 2 3

(5, 6, 7)

{'foo': 1, 'bar': 2}

>>> print_params_4(1, 2)

1 2 3

()

{}

###################################################################

This is more or less the opposite of what we did previously. Instead of gathering the parameters, we want to distribute them. This is simply done by using the * operator at the “other end”—that is, when calling the function rather than when defining it.

def add(x, y): return x + y

params = (1, 2)

 

>>> add(*params)

3

 

 

def hello_3(greeting='Hello', name='world'):

    print '%s, %s!' % (greeting, name)

 

This works with parts of a parameter list, too, as long as the expanded part is last. You can use the same technique with dictionaries, using the ** operator.

>>> params = {'name': 'Sir Robin', 'greeting': 'Well met'}

>>> hello_3(**params)

Well met, Sir Robin!

 

The stars are really useful only if you use them either when defining a function (to allw a varying number of arguments) or when calling a function (to “splice in” a dictionary or a sequence).

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Conda错误:"Collecting package metadata (current_repodata.json): failed"通常是由于网络连接问题导致的。这个错误提示表明在尝试从指定的URL获取包元数据时,发生了HTTP连接错误。这可能是由于网络连接不稳定或阻塞了对指定URL的访问所致。建议您尝试以下解决方法: 1. 确保您的网络连接正常,可以访问互联网。您可以尝试重新启动网络设备,并确保没有任何网络层面的问题。 2. 如果您的网络中阻止了对"https://repo.anaconda.com"的访问,请联系您的网络工程团队,请求他们解决这个问题。 3. 另外,还可以尝试使用conda config命令更改Conda的镜像源为国内的镜像,以加快包的下载速度并避免可能的网络访问问题。可以使用以下命令来更改镜像源: conda config --set show_channel_urls yes conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/ 运行完以上命令后,可以尝试再次运行命令,看是否能够成功获取包的元数据。 4. 如果以上方法都无法解决问题,您还可以尝试手动下载包的元数据文件,并使用本地文件进行安装。可以访问"https://repo.anaconda.com/pkgs/main/win-64/current_repodata.json",下载当前版本的元数据文件,并将其保存在本地。然后,可以使用conda install命令进行本地安装,例如: conda install --offline path/to/current_repodata.json 希望这些解决方法能帮助您解决Conda错误:"Collecting package metadata (current_repodata.json): failed"的问题。如果问题仍然存在,请提供更多详细信息,我们将尽力帮助您解决。<span class="em">1</span><span class="em">2</span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值