python 学习汇总60:类实例作为函数的参数(初级学习- tcy)

 类实例作为函数的参数 2018/11/20 

使用输出参数(通过引用调用)编写函数 :

class callByRef:

def __init__(self, **args):
for (key, value) in args.items():
setattr(self, key, value)

def func(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)

func(args)

print(args.a, args.b)            #new-value 100

猜你喜欢

转载自blog.csdn.net/tcy23456/article/details/84262643
今日推荐