This is a tutorial of how to use *args and **kwargs

For defining the default value of arguments that is not assigned in key words when calling the function:

def func(**keywargs):
if 'my_word' not in keywargs:
word = 'default_msg'
print(word)
else:
word = keywargs['my_word']
print(word)

call this by:

func()
func(my_word='love')

you'll get:

default_msg
love

read more about *args and **kwargs in python: https://www.digitalocean.com/community/tutorials/how-to-use-args-and-kwargs-in-python-3

Add more about default value if arg key is not assigned:

def make_hastie_10_2(n_samples=12000, random_state=None):
"""Generates data for binary classification used in
Hastie et al. 2009, Example 10.2. The ten features are standard independent Gaussian and
the target ``y`` is defined by:: y[i] = 1 if np.sum(X[i] ** 2) > 9.34 else -1 Read more in the :ref:`User Guide <sample_generators>`. Parameters
----------
n_samples : int, optional (default=12000)
The number of samples. random_state : int, RandomState instance or None (default)
Determines random number generation for dataset creation. Pass an int
for reproducible output across multiple function calls.
See :term:`Glossary <random_state>`. Returns
-------
X : array of shape [n_samples, 10]
The input samples. y : array of shape [n_samples]
The output values. References
----------
.. [1] T. Hastie, R. Tibshirani and J. Friedman, "Elements of Statistical
Learning Ed. 2", Springer, 2009. See also
--------
make_gaussian_quantiles: a generalization of this dataset approach
"""
rs = check_random_state(random_state) shape = (n_samples, 10)
X = rs.normal(size=shape).reshape(shape)
y = ((X ** 2.0).sum(axis=1) > 9.34).astype(np.float64, copy=False)
y[y == 0.0] = -1.0 return X, y

kwargs - Key words arguments in python function的更多相关文章

  1. Faiss in python and GPU报错:NotImplementedError: Wrong number or type of arguments for overloaded function 'new_GpuIndexFlatL2'.

    最近在玩faiss,运行这段代码的时候报错了: res = faiss.StandardGpuResources()flat_config = 0index = faiss.GpuIndexFlatL ...

  2. Mock an function to modify partial return value by special arguments on Python

    Mock an function to modify partial return value by special arguments on Python python mock一个带参数的方法,修 ...

  3. Python Function Note

    Python Function Note #汉诺塔问题Python实现 def my_move(n, a, b, c): if n == 1: print(a + ' --> ' + c) el ...

  4. redis学习 (key)键,Python操作redis 键 (二)

    # -*- coding: utf-8 -*- import redis #这个redis 连接不能用,请根据自己的需要修改 r =redis.Redis(host=") 1. delete ...

  5. python function with variadic arguments or keywords(dict) 可变参数与关键字参数

    *args 表示任意个普通参数,调用的时候自动组装为一个tuple **kwags 表示任意个字典类型参数, 调用的时候自动组装成一个dict args和kwags是两个约定俗成的用法. 变长参数可以 ...

  6. python function parameter

    Python 2.7.10 (default, Oct 14 2015, 16:09:02) [GCC 5.2.1 20151010] on linux2 Type "copyright&q ...

  7. python Function

    Python 2.7.10 (default, Oct 14 2015, 16:09:02) [GCC 5.2.1 20151010] on linux2 Type "copyright&q ...

  8. Default arguments and virtual function

    Predict the output of following C++ program. 1 #include <iostream> 2 using namespace std; 3 4 ...

  9. elike.python.function()

    将python用于基本的科学计算,能完全替代matlab.就最近写的一个物理模型程序来看,用python建立的物理模型的可控性,代码的层次性都优于matlab,只不过python没有matlab那样的 ...

随机推荐

  1. weight(搜索对象的选取)

    题目链接: 就是大概这么个东西 根据题意,我们可以清楚的知道:这个题我不会,这个题需要先将2n个数进行排序 这样每对于一个小的前(后)缀和总会在队列最前或队列最后 设这个数为k 那么判断总的Sum(n ...

  2. C/C++判断字符串是否包含某个子字符串

    C风格 #include <iostream> #include <string> #include <cstring> using namespace std; ...

  3. python-网络编程requests模块

    urllib模块去请求的确比较麻烦,需要不断的encode和decode:而requests模块就比较方便了,它是基于requests模块开发的第三方模块,安装简单只需要  pip install r ...

  4. unittest自动化测试举例:自动读取ymal用例&调用接口并生成报告

    用unittest框架写的接口自动化实现过程: 1.编写ymal格式用例: 2.导入ddt模块,该模块的主要功能是帮你读取ymal用例文件,自动获取内容并循环调用函数,具体见代码. 3.导入Beaut ...

  5. sync_binlog innodb_flush_log_at_trx_commit 深入理解

    innodb_flush_log_at_trx_commit和sync_binlog 两个参数是控制MySQL 磁盘写入策略以及数据安全性的关键参数.本文从参数含义,性能,安全角度阐述两个参数为不同的 ...

  6. Docker command line 学习笔记

    deprecated ! 以后直接对这个更新 http://wangzhezhe.github.io/blog/2015/08/10/docker-operations/ 之前整理了好久,每次用到一点 ...

  7. jQ全选或取消全选

    function checkAll(chkobj) {        if ($(chkobj).children("span").text() == "全选" ...

  8. 手把手教你用Pytorch-Transformers——部分源码解读及相关说明(一)

    一.简介 Transformers是一个用于自然语言处理(NLP)的Python第三方库,实现Bert.GPT-2和XLNET等比较新的模型,支持TensorFlow和PyTorch.本文介对这个库进 ...

  9. VS2010中解决Qt“Unable to find a Qt build“

    转自:http://blog.sina.com.cn/s/blog_687960370101d0eu.html 三种方法: 1.在QT菜单下单击OPTION,然后单击ADD,选择QT安装路径. 2.运 ...

  10. poj_3179 Corral the Cows (二分+二维前缀和+离散化)

    [题目链接] http://poj.org/problem?id=3179 [参考] http://www.cnblogs.com/evenbao/p/9243183.html [算法] 二分答案+判 ...