Saturday, May 19, 2018

python - Passing arguments during Class Initialization



I am scratching my head on what is going in the following code.



class foo(object):
def __init__(self,*args):
print type(args)
print args


j_dict = {'rmaNumber':1111, 'caseNo':2222}
print type(j_dict)
p = foo(j_dict)


it yields :






({'rmaNumber': 1111, 'caseNo': 2222},)


It seems to me that this code converts a dict to a tuple!! Can anyone explain this


Answer



Actually it's because args is a tuple to hold a variable length argument list. args[0] is still your dict - no conversion has taken place.



See this tutorial for further information about args and kwargs (the keyword version of args).


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...