Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #6414
| Date | 2011-05-27 10:42 -0700 |
|---|---|
| From | Ethan Furman <ethan@stoneleaf.us> |
| Subject | Re: Python's super() considered super! |
| References | (1 earlier) <87wrhcapm2.fsf@benfinney.id.au> <02172345-6f3b-4fc6-9b88-1c546e3e480a@k15g2000pri.googlegroups.com> <87boyoa5ov.fsf@benfinney.id.au> <4ddf7ed8$0$29996$c3e8da3$5496439d@news.astraweb.com> <Xns9EF278E61F5E6duncanbooth@127.0.0.1> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2156.1306517378.9059.python-list@python.org> (permalink) |
Duncan Booth wrote:
> Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote:
>
>> I was thrilled to learn a new trick, popping keyword arguments before
>> calling super, and wondered why I hadn't thought of that myself. How on
>> earth did I fail to realise that a kwarg dict was mutable and therefore
>> you can remove keyword args, or inject new ones in?
>>
> Probably because most of the time it is better to avoid mutating kwargs.
> Instead of popping an argument you simply declare it as a named argument in
> the method signature. Instead of injecting new ones you can pass them as
> named arguments.
>
>
> def foo(x=None, **kwargs):
> bar(y=2, **kwargs)
>
>
> def bar(**kwargs):
> print(kwargs)
>
>>>> foo(x=1, z=3)
> {'y': 2, 'z': 3}
>>>> foo(x=1, y=2, z=3)
> Traceback (most recent call last):
> File "<pyshell#8>", line 1, in <module>
> foo(x=1, y=2, z=3)
> File "<pyshell#4>", line 2, in foo
> bar(y=2, **kwargs)
> TypeError: bar() got multiple values for keyword argument 'y'
And the above error is exactly why you don't want to use named arguments
in MI -- because you don't know in what order the methods will be
called, you cannot know which named arguments to supply to the method
that super() will call next.
~Ethan~
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Python's super() considered super! Raymond Hettinger <python@rcn.com> - 2011-05-26 09:31 -0700
Re: Python's super() considered super! Raymond Hettinger <python@rcn.com> - 2011-05-26 09:39 -0700
Re: Python's super() considered super! Dotan Cohen <dotancohen@gmail.com> - 2011-05-26 21:13 +0300
Re: Python's super() considered super! Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-26 12:38 -0600
Re: Python's super() considered super! Dotan Cohen <dotancohen@gmail.com> - 2011-05-26 21:56 +0300
Re: Python's super() considered super! Terry Reedy <tjreedy@udel.edu> - 2011-05-26 16:15 -0400
Re: Python's super() considered super! Ben Finney <ben+python@benfinney.id.au> - 2011-05-27 11:39 +1000
Re: Python's super() considered super! Raymond Hettinger <python@rcn.com> - 2011-05-27 00:16 -0700
Re: Python's super() considered super! Ben Finney <ben+python@benfinney.id.au> - 2011-05-27 18:49 +1000
Re: Python's super() considered super! Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-27 10:37 +0000
Re: Python's super() considered super! Duncan Booth <duncan.booth@invalid.invalid> - 2011-05-27 10:53 +0000
Re: Python's super() considered super! Ethan Furman <ethan@stoneleaf.us> - 2011-05-27 10:42 -0700
Re: Python's super() considered super! Duncan Booth <duncan.booth@invalid.invalid> - 2011-05-30 09:18 +0000
RE: Python's super() considered super! "Prasad, Ramit" <ramit.prasad@jpmchase.com> - 2011-05-27 14:10 -0400
Re: Python's super() considered super! Chris Angelico <rosuav@gmail.com> - 2011-05-28 04:40 +1000
Re: Python's super() considered super! sturlamolden <sturlamolden@yahoo.no> - 2011-05-27 07:27 -0700
Re: Python's super() considered super! Mel <mwilson@the-wire.com> - 2011-05-27 10:33 -0400
Re: Python's super() considered super! Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-27 14:49 +0000
Re: Python's super() considered super! harrismh777 <harrismh777@charter.net> - 2011-05-27 10:07 -0500
Re: Python's super() considered super! Duncan Booth <duncan.booth@invalid.invalid> - 2011-05-27 15:05 +0000
Re: Python's super() considered super! sturlamolden <sturlamolden@yahoo.no> - 2011-05-27 08:24 -0700
Re: Python's super() considered super! sturlamolden <sturlamolden@yahoo.no> - 2011-05-27 08:31 -0700
Re: Python's super() considered super! Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-27 16:06 +0000
Re: Python's super() considered super! Stefan Behnel <stefan_ml@behnel.de> - 2011-05-27 23:49 +0200
Re: Python's super() considered super! sturlamolden <sturlamolden@yahoo.no> - 2011-05-27 16:57 -0700
Re: Python's super() considered super! Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-05-28 07:29 +0200
Re: Python's super() considered super! sturlamolden <sturlamolden@yahoo.no> - 2011-05-27 17:04 -0700
Re: Python's super() considered super! Ryan Kelly <ryan@rfk.id.au> - 2011-05-28 09:57 +1000
Re: Python's super() considered super! sturlamolden <sturlamolden@yahoo.no> - 2011-05-27 08:11 -0700
Re: Python's super() considered super! Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-27 12:31 -0600
Re: Python's super() considered super! Chris Angelico <rosuav@gmail.com> - 2011-05-28 04:46 +1000
Re: Python's super() considered super! John Nagle <nagle@animats.com> - 2011-05-27 13:47 -0700
Re: Python's super() considered super! Ethan Furman <ethan@stoneleaf.us> - 2011-05-27 15:24 -0700
csiph-web