Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > fr.comp.lang.python > #4277 > unrolled thread
| Started by | Python <jp@python.invalid> |
|---|---|
| First post | 2025-03-11 18:47 +0000 |
| Last post | 2025-04-19 16:12 +0000 |
| Articles | 3 — 2 participants |
Back to article view | Back to fr.comp.lang.python
Python does what? Python <jp@python.invalid> - 2025-03-11 18:47 +0000
Re: Python does what? Thomas Alexandre <none@no.invalid> - 2025-03-11 19:18 +0000
Re: Python does what? Python <jp@python.invalid> - 2025-04-19 16:12 +0000
| From | Python <jp@python.invalid> |
|---|---|
| Date | 2025-03-11 18:47 +0000 |
| Subject | Python does what? |
| Message-ID | <aB5Wiwv-Cy3MCkoAA9RcNKuT36g@jntp> |
>>> def what(): .. try: .. return 12 .. finally: .. return 42 .. >>> what() ? ? ? 1. Essayer de prévoir 2. Vérifier
[toc] | [next] | [standalone]
| From | Thomas Alexandre <none@no.invalid> |
|---|---|
| Date | 2025-03-11 19:18 +0000 |
| Message-ID | <67d08c89$0$5196$426a74cc@news.free.fr> |
| In reply to | #4277 |
Le Tue, 11 Mar 25 18:47:54 +0000, Python a écrit : >>>> def what(): > .. try: > .. return 12 > .. finally: > .. return 42 .. >>>> what() > ? ? ? > > 1. Essayer de prévoir 42 > 2. Vérifier ``` The return value of a function is determined by the last return statement executed. Since the finally clause always executes, a return statement executed in the finally clause will always be the last one executed ``` https://docs.python.org/3/reference/compound_stmts.html#finally-clause Oui, c'est totalement contre-intuitif - particulièrement dans cet exemple. -- "Ce qu'il faut au fond pour obtenir une espèce de paix avec les hommes, (...) c'est leur permettre en toutes circonstances, de s'étaler, de se vautrer parmi les vantardises niaises. Il n'y a pas de vanité intelligente. C'est un instinct." - Céline
[toc] | [prev] | [next] | [standalone]
| From | Python <jp@python.invalid> |
|---|---|
| Date | 2025-04-19 16:12 +0000 |
| Message-ID | <qkXiBsNSqQznGMXUYakD1nUMo5w@jntp> |
| In reply to | #4278 |
Le 11/03/2025 à 21:18, Thomas Alexandre a écrit :
> Le Tue, 11 Mar 25 18:47:54 +0000, Python a écrit :
>
>>>>> def what():
>> .. try:
>> .. return 12
>> .. finally:
>> .. return 42 ..
>>>>> what()
>> ? ? ?
>>
>> 1. Essayer de prévoir
>
> 42
>
>> 2. Vérifier
>
> ```
> The return value of a function is determined by the last return statement
> executed. Since the finally clause always executes, a return statement
> executed in the finally clause will always be the last one executed
> ```
> https://docs.python.org/3/reference/compound_stmts.html#finally-clause
>
> Oui, c'est totalement contre-intuitif - particulièrement dans cet exemple.
$ cat what.py
#!/usr/bin/env python3
try:
exit(0)
finally:
exit(42)
$ ./what.py ; echo $?
42
[toc] | [prev] | [standalone]
Back to top | Article view | fr.comp.lang.python
csiph-web