Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #90346
| From | Michael Welle <mwe012008@gmx.net> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: functions, optional parameters |
| Date | 2015-05-11 07:58 +0200 |
| Organization | MB-NET.NET for Open-News-Network e.V. |
| Message-ID | <h1t52cxd15.ln2@news.c0t0d0s0.de> (permalink) |
| References | <72lu1cxvmg.ln2@news.c0t0d0s0.de> <554cd511$0$12979$c3e8da3$5496439d@news.astraweb.com> <ui2v1cx5co.ln2@news.c0t0d0s0.de> <554ec664$0$13001$c3e8da3$5496439d@news.astraweb.com> |
Hello,
Steven D'Aprano <steve+comp.lang.python@pearwood.info> writes:
> On Sat, 9 May 2015 01:50 am, Michael Welle wrote:
[...]
>>> How about this definition:
>>>
>>> default = 23
>>> def spam(eggs=default):
>>> pass
>>>
>>> del default
>>>
>>> print spam()
>>>
>>>
>>> Do you expect the function call to fail because `default` doesn't exist?
>>
>> If I reference an object, that isn't available in the current context, I
>> want to see it fail, yes.
>
> Well, that's an interesting response. Of course I agree with you if the
> reference to default is in the code being executed:
Uhhm, I change my mind ;). I overread the del statement and interpreted
the example different. I guess, I share your opinion on this example.
[due to quota limit of the news server I had to delete more text than I wished ]
> thunk = lambda: 0
> value = thunk()
>
>
> Both of those are considerably slower than the current behaviour:
>
> py> from timeit import Timer
> py> static = Timer("x = 0")
> py> thunk = Timer("x = eval(t)", setup="t = compile('0', '', 'eval')")
> py> func = Timer("x = f()", setup="f = lambda: 0")
> py> min(static.repeat(repeat=7)) # Best of seven trials.
> 0.04563648998737335
> py> min(thunk.repeat(repeat=7))
> 1.2324241530150175
> py> min(func.repeat(repeat=7))
> 0.20116623677313328
I'm not so deep into the internals of the Python interpreter, CPython in
most cases I think, but I wonder if the generated byte code for the eval
statement from above and a hypothetic function call with late binding
has to be the same? I guess eval comes with some startup costs?
[...]
> This is the point where some people try to suggest some sort of complicated,
> fragile, DWIM heuristic where the compiler tries to guess whether the user
> actually wants the default to use early or late binding, based on what the
> expression looks like. "0 is an immutable int, use early binding; [] is a
> mutable list, use late binding." sort of thing. Such a thing might work
> well for the obvious cases, but it would be a bugger to debug and
> work-around for the non-obvious cases when it guesses wrong -- and it will.
Well, we all want a mind reading compiler, that spits out the code we
have thought about, don't we ;).
Thanks for the insights in the posting, I appreciate it.
Regards
hmw
--
biff4emacsen - A biff-like tool for (X)Emacs
http://www.c0t0d0s0.de/biff4emacsen/biff4emacsen.html
Flood - Your friendly network packet generator
http://www.c0t0d0s0.de/flood/flood.html
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
functions, optional parameters Michael Welle <mwe012008@gmx.net> - 2015-05-08 13:59 +0200
Re: functions, optional parameters Rustom Mody <rustompmody@gmail.com> - 2015-05-08 05:09 -0700
Re: functions, optional parameters Michael Welle <mwe012008@gmx.net> - 2015-05-08 14:36 +0200
Re: functions, optional parameters Chris Angelico <rosuav@gmail.com> - 2015-05-08 22:39 +1000
Re: functions, optional parameters Michael Welle <mwe012008@gmx.net> - 2015-05-08 14:57 +0200
Re: functions, optional parameters Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-09 01:24 +1000
Re: functions, optional parameters Chris Angelico <rosuav@gmail.com> - 2015-05-09 02:02 +1000
Re: functions, optional parameters Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-09 03:36 +1000
Re: functions, optional parameters Chris Angelico <rosuav@gmail.com> - 2015-05-09 03:49 +1000
Re: functions, optional parameters Mel Wilson <mwilson@the-wire.com> - 2015-05-08 18:49 +0000
Re: functions, optional parameters Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-05-09 13:41 +1200
Re: functions, optional parameters Chris Angelico <rosuav@gmail.com> - 2015-05-09 12:05 +1000
Re: functions, optional parameters Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-05-09 19:27 +1200
Re: functions, optional parameters Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-09 12:52 +1000
Re: functions, optional parameters Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-09 03:26 +1000
Re: functions, optional parameters Michael Welle <mwe012008@gmx.net> - 2015-05-08 17:50 +0200
Re: functions, optional parameters Ian Kelly <ian.g.kelly@gmail.com> - 2015-05-09 10:57 -0600
Re: functions, optional parameters Chris Angelico <rosuav@gmail.com> - 2015-05-10 13:33 +1000
Re: functions, optional parameters Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-10 15:20 +1000
Re: functions, optional parameters Chris Angelico <rosuav@gmail.com> - 2015-05-10 18:59 +1000
Re: functions, optional parameters Rustom Mody <rustompmody@gmail.com> - 2015-05-09 20:35 -0700
Re: functions, optional parameters Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-10 15:25 +1000
Re: functions, optional parameters Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-10 12:45 +1000
Re: functions, optional parameters Dave Angel <davea@davea.name> - 2015-05-10 07:25 -0400
Re: functions, optional parameters Chris Angelico <rosuav@gmail.com> - 2015-05-11 00:39 +1000
Re: functions, optional parameters Michael Welle <mwe012008@gmx.net> - 2015-05-11 07:58 +0200
Re: functions, optional parameters Ian Kelly <ian.g.kelly@gmail.com> - 2015-05-08 09:48 -0600
csiph-web