One way to think about this is to remember the rules for function calls.
You take the function body and you evaluate it in the new argument.
So the body of sayhello is print greeting + " " person,
and we already know from seeing it in the interpreter what it's going to print out.
It's going to print out "hello from u.p." "gracie".
Currently I don't see gracie anywhere in this environment.
That suggests that we're definitely going to need to add it.
Another way to get to that is to remember the other rules for constructing environments.
We definitely want to make space for the formal parameters, person,
and put in the value of the actual argument, "gracie".
So now when we go to evaluate greeting, we don't see it here
but we'll go one up and get "hello from u.p."
and we'll try to find person. We see it right here. We get "gracie".
We'll compose them together and print out exactly the same behavior
we saw on the interpreter. This quiz was pretty tricky.
This notion of nested procedures does not come up very often in Python.
But if we want a complete interpreter, one that understands all the nuances of a language,
then we have to handle this.
It's kind of like the subjunctive in a lot of romance languages.
It doesn't come up very often in English; may come up more often than you'd think in other places.
考え方の1つとして関数を呼び出す時の
規則を思い出してみるとよいでしょう
関数の中身を新しい引数の環境で評価します
sayhelloの中身はgreeting+“”+personですが
インタプリタで見たとおり
何を出力するのかは知っています
“hello from u.p.”
“gracie”を出力するはずです
今はまだ“gracie”が環境にないので
加える必要があると推測できます
環境を作る時の規則を
思い出してみてもいいでしょう
personという仮引数の場所を作りたいので
そこに実引数の値“gracie”を入れます
greetingを評価する時 ここにはないので
1つ上を見て“hello from u.p.”を得ます
その後personを捜す時はここにあるので
“gracie”が得られます
これらをつなげ
先ほどインタプリタで見たとおりの出力をします
この小テストは なかなか難しかったですね
入れ子になった関数は
Pythonではあまり使われませんが
言語のすべてを理解するような
完全なインタプリタを作りたいなら
扱えなければなりません
ロマンス語圏の仮定法のようなものです
英語ではあまり使われませんが
他の場所ではよく使われます
Uma maneira de pensr sobre isso é lembrar-se das regras de chamadas de função.
Você avalia o corpo da função em um novo ambiente,
O corpo de sayhello é: print greeting + " " + person,
e já sabemos, do que vimos no interpretador, o que isso vai imprimir --
vai imprimir "hello from up gracie".
Correntemente, eu não vejo "gracie" em lugar algum deste ambiente.
Isso sugere que definitivamente vamos precisar adicionar isto.
Outra maneira de chegar a isso é lembrar-se de outras regras de construção de ambientes.
Definitivamente queremos alocar espaço para o parâmetro formal -- person --
e colocar aí o valor do argumento -- "gracie".
Então, agora, quando vamos avaliar greeting, não o vemos aqui,
mas voltamos para cima e obtemos "hello from up",
e vamos tentar encontrar person. Vemos isso logo aqui -- obtemos "gracie".
Compomos os dois e imprimimos exatamente o mesmo
que vimos com o interpretador. Este teste foi bem difícil.
Essa noção de funções aninhadas não ocorre muito frequentemente em Python.
Mas se quisermos um interpretador completo, um que entenda todas as nuances da linguagem,
vamos ter que tratar isso.
É como o subjuntivo de muitas linguagens de romances:
ele não ocorre com frequência em inglês, mas pode ocorrer mais frequentemente do que você imagina, em outros lugares.