3 Replies - 4656 Views - Last Post: 14 January 2013 - 03:54 PM Rate Topic: -----

#1 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 203
  • View blog
  • Posts: 1,731
  • Joined: 13-March 10

Packing and Unpacking arguments

Posted 14 January 2013 - 01:38 PM

I get the whole concept of packing and unpacking arguments but i have 2 questions regarding it.
First, can anyone provide a real-world use case when packing of arguments would be better than - let's say - passing a list?
Second, when the arguments are actually packed, in what data structure are the stored. Because when printed, it looks like tuple because:

def printMe(*args):


#Output: (1,2,3,4,5)

output looks very tuple-like but we can actually modify the arguments inside the function (tuples are supposed to be immutatble).


Is This A Good Question/Topic? 0
  • +

Replies To: Packing and Unpacking arguments

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2576
  • View blog
  • Posts: 4,110
  • Joined: 21-June 11

Re: Packing and Unpacking arguments

Posted 14 January 2013 - 01:58 PM

1: One real-world example would be python's own print (in Python 3 that is - in Python 2 print is a keyword, not a function). If you'd have to type something print(["Hello World"]) that could get pretty annoying. I mean it's just two additional characters, but still.

Generally I'd say it makes sense to use varags when the user is much more likely to call the function with a list literal (i.e. f([x,y,z]) than with a list variable (i.e. f(my_list)).

2: The type of args is indeed tuple and you can't mutate it. That is when I change your code like this:

def printMe(*args):
   args[0] = 42


I get a type error telling me that "'tuple' object does not support item assignment".

PS: When you want to find out the type of an object, you can just call type(some_object).
Was This Post Helpful? 3
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5186
  • View blog
  • Posts: 13,914
  • Joined: 18-April 07

Re: Packing and Unpacking arguments

Posted 14 January 2013 - 02:02 PM

When they are packed up they are packed up in a tuple. You will need to unpack them in order to use them. This might be advantageous if say the arguments for the function itself are in a list. Perhaps the list specifies the start and end of a range of numbers. Passing the list to the range() function would be passing a list, but passing a list which has been packaged up will then unpacked and its individual elements will be used as the parameters to the function.

It would be like saying to someone "Here, take this box of items and use them". They would have to take the box, open it up and use the items they found inside. Now with packing I would say "here, use these items in this box" and unpack the box, give them the items and then they would use them.

You usually find packing/unpacking as a simple syntax for providing parameters to a function, not passing an object containing parameters. Hope that makes sense. :)
Was This Post Helpful? 1
  • +
  • -

#4 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 165
  • View blog
  • Posts: 258
  • Joined: 19-July 09

Re: Packing and Unpacking arguments

Posted 14 January 2013 - 03:54 PM

Well, what are you going to do if you dont know the function signature? Like in a decorator. A toy example, the memoize decorator:

def memoize(func):
    #we cache previously computed results of functions decorated with this
    #only works for functins whose arguments can be dictionary keys
    cache = {}
    def _memoize(*args): #we dont know the function signature so use *args
        if args in cache:
            return cache[args]
            res = func(*args) #same here
            cache[args] = res
            return res
    return _memoize

def fib(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
        return fib(n-1) + fib(n-2)

def silly(n, m):
    return n + m

print(silly(30, 30))

This post has been edited by Nallo: 14 January 2013 - 03:57 PM

Was This Post Helpful? 4
  • +
  • -

Page 1 of 1