var_args.py 483 B

123456789101112131415
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. def hello(greeting, *args):
  4. if (len(args)==0):
  5. print('%s!' % greeting)
  6. else:
  7. print('%s, %s!' % (greeting, ', '.join(args)))
  8. hello('Hi') # => greeting='Hi', args=()
  9. hello('Hi', 'Sarah') # => greeting='Hi', args=('Sarah')
  10. hello('Hello', 'Michael', 'Bob', 'Adam') # => greeting='Hello', args=('Michael', 'Bob', 'Adam')
  11. names = ('Bart', 'Lisa')
  12. hello('Hello', *names) # => greeting='Hello', args=('Bart', 'Lisa')