python 像C语言样的函数参数引用实现..
2014-02-25 12:29
387 查看
python中有传参需求,好像也没有像C一样传参(引用)设计,可以采用一种变形方式,函数返回值来实现。
这种方式不是很好看,还可以采用其他的方法:
1) 上面说的函数返回值,可以说成使用元祖返回的方式。
2)通过可变化的对象(list)
3) 通过字典(dict)
4)通过类的对象
def test(): t1 = "123123" t2 = "test" t3 = 122 t4 = 1.12 return t1, t2, t3, t4 t1, t2, t3, t4 = test() print t1, t2, t3, t4
这种方式不是很好看,还可以采用其他的方法:
1) 上面说的函数返回值,可以说成使用元祖返回的方式。
2)通过可变化的对象(list)
def func1(a): a[0] = 'new-value' # 'a' references a mutable list a[1] = a[1] + 1 # changes a shared object
3) 通过字典(dict)
def func3(args): args['a'] = 'new-value' # args is a mutable dictionary args['b'] = args['b'] + 1 # change it in-place
4)通过类的对象
class callByRef: def __init__(self, **args): for (key, value) in args.items()://args是个dict setattr(self, key, value)//相当于 self.key=value def func4(args): args.a = 'new-value' # args is a mutable callByRef args.b = args.b + 1 # change object in-place args = callByRef(a='old-value', b=99) func4(args) print args.a, args.b
相关文章推荐
- 利用ida python 实现复原函数调用的参数 (仅对数据被简单硬编码有效)
- Python的函数参数传递:传值?引用?
- Python的函数参数传递:传值?引用?
- python 函数参数传值还是传引用
- Python def函数的定义、使用及参数传递实现代码
- python函数参数引用传递
- <python>Python的函数参数传递:传值?引用?
- Python可变参数函数的实现
- Python 函数参数引用(传值/传址)/copy/deepcopy
- Python的函数参数传递:传值?引用?
- Python 函数参数引用(传值/传址)/copy/deepcopy
- Python中在脚本中引用其他文件函数的实现方法
- Python:函数参数传递:传值?引用?
- c++将引用或者是指针作为函数参数实现实参的运算
- Python中在脚本中引用其他文件函数的实现方法
- Python的函数参数传递:传值?引用?
- Python函数参数到底是按值还是按引用
- Python函数参数传递:传值还是传引用
- Bullet:Python的函数中参数是引用吗?
- Python实现接受任意个数参数的函数方法