#1
  1. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    620
    Rep Power
    65

    Red face Function Parameter


    What does an asterisk in front of a function parameter mean?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    if remember correctly: * = a list, ** = a dictionary

    Code:
    >>> def lis(*s):
    	print s
    
    	
    >>> lis(2,3,4,5,6,7,8,9)
    (2, 3, 4, 5, 6, 7, 8, 9)
    >>> 
    >>> def dic(**d):
    	print d
    
    	
    >>> dic(a=1)
    {'a': 1}
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Atlantic City, NJ
    Posts
    327
    Rep Power
    13
    * is a tuple and ** is a dictionary.
    I'll learn this stuff someday.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    Oh ya sorry it is a tuple :P

    I never use it no wonder
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    620
    Rep Power
    65

    Smile


    Thanks, couldn't find any reference in the help file.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    what is it used for? you can pass a tuple or a dictionary without defining that oyu are wanting one?
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    You use it when you want a program to be able to take any number of parameters, either positional (using *) or keywords (using **).

    Here is an example using positional parameters. The function 'average' returns the average of all the parameters.
    Code:
    >>> def average(*params):
    ...     return sum(params)/ float(len(params))
    ...
    >>> average(1,2,3,4,25)
    7.0
    >>> average(1,2,3,4,25,100)
    22.5
    You can also use * and ** when calling functions. This converts a sequence (*) or dictionary (**) into positional and keyword parameters respectively.
    e.g.
    Code:
    >>> x = [1,2,3,4,25,100]
    >>> average( *x )
    22.5
    Dave - The Developers' Coach

IMN logo majestic logo threadwatch logo seochat tools logo