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. Flask框架—flask_sqlalchemy组件使用

    一.flask_sqlalchemy组件 我们之前学过SQLAlchemy,一个独立的数据库关系对象映射,其实在flask中也有官方认可的第三方SQLAlchemy组件,用于处理flask中对象关系映 ...

  2. CSS3------box-shadow,即单边阴影效果设置

    box-shadow修改元素的阴影效果要方便得多,因为box-shadow可以修改六个参数,得到不同的效果.下面结合一些简单的案例来对box-shadow属性进行演示说明. 1.单边阴影效果 定义元素 ...

  3. 《Python Data Structures》 Week4 List 课堂笔记

    Coursera课程<Python Data Structures> 密歇根大学 Charles Severance Week4 List 8.2 Manipulating Lists 8 ...

  4. POI向Excel中写入数据及追加数据

    import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import ...

  5. python nginx+uwsgi+WSGI 处理请求详解

    https://blog.csdn.net/a519640026/article/details/76157976 请求从 Nginx 到 uwsgi 到 django 交互概览 作为python w ...

  6. GitHub Pages建立第一个静态页面

    1.创建仓库 创建仓库点击右上角的加号,选择newrepository.然后对仓库信息进行设置.注意箭头标识的几个地方.仓库名必须是http://username.github.io的形式.必须勾选p ...

  7. Python:库文件

    可以引用的Python文件:自己写的.py + 外部库(external libraries)

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

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

  9. python的继承、重载和重写???

    继承语法:<1>单继承:class(父类名)<2>多继承class(父类1,父类2,父类n...) 继承的特点:<1>减少代码量和灵活指定型类<2>子类 ...

  10. 2101 可达性统计(拓扑排序/dfs+状态压缩)

    [题目描述] 给定一张N个点M条边的有向无环图,分别统计从每个点出发能够到达的点的数量.N,M≤30000. [题目链接] 2101 可达性统计 [算法] 拓扑排序之后逆序计算(感觉dfs更好写而且应 ...