Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #68346
| References | (4 earlier) <roy-493F5F.07442713032014@news.panix.com> <87y50el1vf.fsf@elektro.pacujo.net> <5322420e$0$29994$c3e8da3$5496439d@news.astraweb.com> <CAPTjJmpufKDDnWRBusutSR4hdxVu8LvmBp9SXjYJmBjy7rx+4Q@mail.gmail.com> <CALwzidkG1GdOH5PQDEzaj76aNW2OWCMXYkg4_xth38jtnvBJ9g@mail.gmail.com> |
|---|---|
| Date | 2014-03-14 11:22 +1100 |
| Subject | Re: Deep vs. shallow copy? |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.8137.1394756567.18130.python-list@python.org> (permalink) |
On Fri, Mar 14, 2014 at 11:08 AM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> On Thu, Mar 13, 2014 at 5:55 PM, Chris Angelico <rosuav@gmail.com> wrote:
>> I'm going to troll for a moment and give you a function that has no
>> return value.
>>
>> def procedure():
>> raise Exception
>
>>>> import dis
>>>> dis.dis(procedure)
> 2 0 LOAD_GLOBAL 0 (Exception)
> 3 RAISE_VARARGS 1
> 6 LOAD_CONST 0 (None)
> 9 RETURN_VALUE
That's a return value in the same way that exec() has a return value
[1]. If somehow the raise fails, it'll return None.
>>>> def get_procedure_return_value():
> ... """Returns the return value of procedure()."""
> ... return procedure.__code__.co_consts[0]
> ...
>>>> print(get_procedure_return_value())
> None
>
> Look, there it is!
Succeeds by coincidence. From what I can see, *every* CPython function
has const slot 0 dedicated to None. At least, I haven't been able to
do otherwise.
>>> def function(x):
return x*2+1
>>> import dis
>>> dis.dis(function)
2 0 LOAD_FAST 0 (x)
3 LOAD_CONST 1 (2)
6 BINARY_MULTIPLY
7 LOAD_CONST 2 (1)
10 BINARY_ADD
11 RETURN_VALUE
>>> function.__code__.co_consts
(None, 2, 1)
Your return value retriever would say it returns None still, but it doesn't.
Trollbridge: you have to pay a troll to cross.
ChrisA
[1] I'm not talking about Python's 'exec' statement, but about the
Unix exec() API, eg execlpe() - see http://linux.die.net/man/3/exec
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Deep vs. shallow copy? Alex van der Spek <zdoor@xs4all.nl> - 2014-03-12 14:25 +0000
Re: Deep vs. shallow copy? Skip Montanaro <skip@pobox.com> - 2014-03-12 09:48 -0500
Re: Deep vs. shallow copy? Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-03-12 10:00 -0500
Re: Deep vs. shallow copy? Alex van der Spek <zdoor@xs4all.nl> - 2014-03-12 15:29 +0000
Re: Deep vs. shallow copy? Wayne Brehaut <wbrehaut@mcsnet.ca> - 2014-03-12 13:06 -0600
Re: Deep vs. shallow copy? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-12 23:07 +0000
Re: Deep vs. shallow copy? Rustom Mody <rustompmody@gmail.com> - 2014-03-12 20:09 -0700
Re: Deep vs. shallow copy? Ian <hobson42@gmail.com> - 2014-03-13 11:38 +0000
Re: Deep vs. shallow copy? Rustom Mody <rustompmody@gmail.com> - 2014-03-13 08:28 -0700
Re: Deep vs. shallow copy? random832@fastmail.us - 2014-03-13 13:25 -0400
Re: Deep vs. shallow copy? Roy Smith <roy@panix.com> - 2014-03-13 07:44 -0400
Re: Deep vs. shallow copy? Marko Rauhamaa <marko@pacujo.net> - 2014-03-13 14:27 +0200
Re: Deep vs. shallow copy? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-13 23:41 +0000
Re: Deep vs. shallow copy? Chris Angelico <rosuav@gmail.com> - 2014-03-14 10:55 +1100
Re: Deep vs. shallow copy? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-14 01:41 +0000
Re: Deep vs. shallow copy? Ian Kelly <ian.g.kelly@gmail.com> - 2014-03-13 18:08 -0600
Re: Deep vs. shallow copy? Chris Angelico <rosuav@gmail.com> - 2014-03-14 11:22 +1100
Re: Deep vs. shallow copy? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-14 01:59 +0000
Re: Deep vs. shallow copy? Rustom Mody <rustompmody@gmail.com> - 2014-03-13 19:57 -0700
Re: Deep vs. shallow copy? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-14 04:43 +0000
Re: Deep vs. shallow copy? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-14 06:17 +0000
Re: Deep vs. shallow copy? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-13 23:31 +0000
csiph-web