Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #20553
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.glorb.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail |
|---|---|
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: question about function pointer |
| Date | Fri, 17 Feb 2012 05:20:52 -0800 (PST) |
| Organization | http://groups.google.com |
| Lines | 63 |
| Message-ID | <11998859.481.1329484852059.JavaMail.geo-discussion-forums@pbux2> (permalink) |
| References | <mailman.5911.1329465188.27778.python-list@python.org> <pan.2012.02.17.09.55.11.504000@nowhere.com> |
| NNTP-Posting-Host | 112.105.144.164 |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | quoted-printable |
| X-Trace | posting.google.com 1329484852 11447 127.0.0.1 (17 Feb 2012 13:20:52 GMT) |
| X-Complaints-To | groups-abuse@google.com |
| NNTP-Posting-Date | Fri, 17 Feb 2012 13:20:52 +0000 (UTC) |
| In-Reply-To | <pan.2012.02.17.09.55.11.504000@nowhere.com> |
| Complaints-To | groups-abuse@google.com |
| Injection-Info | glegroupsg2000goo.googlegroups.com; posting-host=112.105.144.164; posting-account=5JdMBQoAAABHnS4mjpqEzxnmWtgiiVNw |
| User-Agent | G2/1.0 |
| X-Google-Web-Client | true |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:20553 |
Show key headers only | View raw
在 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.
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
question about function pointer Zheng Li <dllizheng@gmail.com> - 2012-02-17 16:53 +0900
Re: question about function pointer Nobody <nobody@nowhere.com> - 2012-02-17 09:55 +0000
Re: question about function pointer 88888 Dihedral <dihedral88888@googlemail.com> - 2012-02-17 05:20 -0800
csiph-web