question about function pointer

88888 Dihedral dihedral88888 at googlemail.com
Fri Feb 17 08:20:52 EST 2012


在 2012年2月17日星期五UTC+8下午5时55分11秒,Nobody写道:
> On Fri, 17 Feb 2012 16:53:00 +0900, Zheng Li wrote:
> 
> > def method1(a = None):
> > 	print a
> > 
> > i can call it by
> > method1(*(), **{'a' : 1})
> > 
> > I am just curious why it works and how it works?
> > and what do *() and **{'a' : 1} mean?
> 
> In a function call, an argument consisting of * followed by an expression
> of tuple type inserts the tuple's elements as individual positional
> arguments. An argument consisting of ** followed by an expression of
> dictionary type inserts the dictionary's elements as individual keyword
> arguments.
> 
> So if you have:
> 
> 	a = (1,2,3)
> 	b = {'a': 1, 'b': 2, 'c': 3}
> 
> then:
> 
> 	func(*a, **b)
> 
> is equivalent to:
> 
> 	func(1, 2, 3, a = 1, b = 2, c = 3)
> 
> > when I type *() in python shell, error below happens
> > 
> >   File "<stdin>", line 1
> >     *()
> >     ^
> > SyntaxError: invalid syntax
> 
> The syntax described above is only valid within function calls.
> 
> There is a similar syntax for function declarations which does the reverse:
> 
> 	> def func(*a, **b):
> 	    print a
> 	    print b
> 
> 	> func(1, 2, 3, a = 1, b = 2, c = 3)
> 	(1, 2, 3)
> 	{'a': 1, 'c': 3, 'b': 2}

In the functional programming view, 2 to 5
object parameters are enough to invoke a function.
 
But the tuple and dictionary packing and unpacking  
are not free in the run time.


Enhancement to operations of  basic types in Python can speed up everything.  




More information about the Python-list mailing list