理解*arg 、**kwargs
这两个是python中的可变参数。*args表示任何多个无名参数,它是一个tuple(元祖);**kwargs表示关键字参数,它是一个dict(字典)。
并且同时使用*args和**kwargs时,必须*args参数列要在**kwargs前。看下面例子:
# https://godjango.com/105-understanding-args-and-kwargs/(例子来源)
1 def print_args(d1, d2, d3):
print(d1, d2, d3) data = ('foo', 'bar', 'baz')
print_args(*data) # 在data之前使用一个星号,表示让函数接受任意多的位置参数。
>>> foo bar baz def print_args(*args):
for arg in args:
print(arg)
print_args(1, 2, 3)
>>> 1
>>> 2
>>> 3 def print_args(*args):
print(args)
print_args(1, 2, 3)
>>> (1, 2, 3) def print_args(a, b, c, *args):
print(a, b, c, args)
print_args(1, 2, 3, 4, 5)
>>> 1 2 3 (4, 5) def print_kwargs(**kwargs): # 在参数名之前使用2个星号来支持任意多的关键字参数
print(kwargs)
print_kwargs(foo='bar', hello='world')
>>> {'foo': 'bar', 'hello': 'world'} def print_kwargs(latitude=None, longitude=None):
print(latitude, longitude) data = {'latitude': 0.00, 'longitude': 1.00}
print_data(**data)
>>> 0.00 1.00 def print_kwargs(lat=None, long=None, **kwargs):
print(lat, long, kwargs)
print_kwargs(1, 2, data='other')
>>> 1 2 {'data': 'other'}
Python super()函数用法
super() 函数是用于调用父类(超类)的一个方法。好处是可以避免直接调用父类的名字
值得注意的是,在python3中直接使用 super().xxx 代替 super(Class, self).xxx (Python2格式)
# http://www.runoob.com/python/python-func-super.html(例子来源)
1 class FooParent(object):
def __init__(self):
self.parent = 'I\'m the parent.'
print ('Parent') def bar(self,message):
print ("%s from Parent" % message) class FooChild(FooParent):
def __init__(self):
# super(FooChild,self) 首先找到 FooChild 的父类(就是类 FooParent),然后把类B的对象 FooChild 转换为类 FooParent 的对象
super(FooChild,self).__init__()
print ('Child') def bar(self,message):
super(FooChild, self).bar(message)
print ('Child bar fuction')
print (self.parent) if __name__ == '__main__':
fooChild = FooChild()
fooChild.bar('HelloWorld')
输出为:
Parent
Child
HelloWorld from Parent
Child bar fuction
I'm the parent.
理解*arg 、**kwargs的更多相关文章
- 装饰器,装饰器多参数的使用(*arg, **kwargs),装饰器的调用顺序
一.#1.执行outer函数,并且将其下面的函数名,当作参数 #2.将outer的返回值重新赋值给f1 = outer的返回值 #3.新f1 = inner #4.func = 原f1 #!/usr/ ...
- python学习-- 理解'*','*args','**','**kwargs'
刚开始学习Python的时候,对有关args,kwargs,和*的使用感到很困惑.相信对此感到疑惑的人也有很多.我打算通过这个帖子来排解这个疑惑(希望能减少疑惑). 让我们通过以下5步来理解: 1. ...
- Python开发 基礎知識 2.變量 ( *arg, **kwargs )
變量 *args 和 **kwargs ( *和**為本體,名稱為通俗的名稱約定 ) *args 用於函式定義. 可將不定數量的參數傳遞給一個函數,傳入函式的引數,會先以Tuple物件收集,再設定給參 ...
- *arg,**kwargs的参数作用的疑惑
先来看个例子: def foo(*args, **kwargs): print 'args = ', args print 'kwargs = ', kwargs print '----------- ...
- 【Python】装饰器理解
以下文章转载自:点这里 关于装饰器相关的帖子记录在这里: 廖雪峰, thy专栏, stackflow Python的函数是对象 简单的例子: def shout(word="yes" ...
- python中“生成器”、“迭代器”、“闭包”、“装饰器”的深入理解
python中"生成器"."迭代器"."闭包"."装饰器"的深入理解 一.生成器 1.生成器定义:在python中,一边 ...
- 深入理解 Python 中的装饰器
装饰器本质上也是函数,接收函数对象来作为参数,并在装饰器的内部来调用接受的函数对象完成相关的函数调用,也可以这样理解 ,为了方便在几个不同函数调用之前或者完成相关的统一操作,注意是完成统一的操作, ...
- 【Python】Python中*args 和**kwargs的用法
好久没有学习Python了,应为工作的需要,再次拾起python,唤起记忆. 当函数的参数不确定时,可以使用*args 和**kwargs,*args 没有key值,**kwargs有key值. 还是 ...
- 在代理中托管特殊方法的python代码实现
任务简单的介绍是: 在新风格对象模型中,Python操作其实是在类中查找特殊方法的(经典对象是在实例中进行操作的),现在需要将一些新风格的实例包装到代理中,,此代理可以选择将一些特殊的方法委托给内部的 ...
随机推荐
- Recurrent neural network (RNN) - Pytorch版
import torch import torch.nn as nn import torchvision import torchvision.transforms as transforms # ...
- ABP中的本地化处理(下)
在上篇文章中我们的重点是讲述怎样通过在Domain层通过PreInitialize()配置ILocalizationConfiguration中的Sources(IList<ILocalizat ...
- cesium 水面、淹没 效果
水面效果 参考: http://cesiumcn.org/topic/158.html http://api.rivermap.cn/cesium/rivermap/map.html https:// ...
- I2C基础及时序
1.模式 标准模式:达到100Kb/S 快速模式:达到400Kb/S 2.连接图 3.协议 SDA.SCL在空闲的时候为高电平 重点!重点!重点! 4.涉及到多主机仲裁的竞争及时钟信号的同步
- vue的生命周期 created mounted等
生命周期: beforeCreate 在实例初始化之后,数据观测和event/watcher时间配置之前被调用 created 页面加载之前执行,在实例创建完成后被立即调用.执行顺序:父组件-子组件 ...
- Kettle部署笔记
1.启动脚本(启动job) /u02/www/data-integration/kitchen.sh -file:/u02/www/data-integration/job.kjb -logfile= ...
- C# 不是序列化xml 转实体Model【原家独创】
public static T XmlConvertModel<T>(string xmlStr) where T : class, new() { T ...
- 【转载】Sqlserver存储过程中使用Select和Set给变量赋值
Sqlserver存储过程是时常使用到的一个数据库对象,在存储过程中会使用到Declare来定义存储过程变量,定义的存储过程变量可以通过Set或者Select等关键字方法来进行赋值操作,使用Set对存 ...
- 【转载】Sqlserver使用SUBSTRING函数截取字符串
在SQL语句查询过程中,Sqlserver支持使用LEFT().RIGHT().SUBSTRING()等几个函数对字符串进行截取操作,SubString函数相对于其他两个函数来说更灵活,使用场景更多, ...
- c# dynamic实现动态实体,不用定义实体就能序列化为标准json
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...