The extra credit question for this exercise:
本练习的额外信用问题:
Q: Why do you you call the variable
jelly_beans
but the namebeans
later?问:为什么你之后调用变量jelly_beans但名称bean?
A: That's part of how a function works. Remember that inside the function the variable is temporary. When you return it then it can be assigned to a variable for later. I'm just making a new variable named
beans
to hold the return value.答:这是功能如何运作的一部分。请记住,在函数内部,变量是临时的。当您返回它时,可以将其分配给变量以供日后使用。我只是创建一个名为beans的新变量来保存返回值。
What does "the variable inside the function is temporary" mean? Does that mean the variable is not valid after the return
? It seems like after the indentation of the function, I can't print the variable used in the function part.
“函数内部的变量是暂时的”是什么意思?这是否意味着该变量在返回后无效?似乎在函数缩进之后,我无法打印函数部分中使用的变量。
From the answer it says "When you return it then it can be assigned to a variable for later". Could someone explain that sentence a bit please?
从答案中可以看出“当你返回它时,它可以被分配给一个变量供以后使用”。有人可以解释一下这句话吗?
print "Let's practice everything."
print 'You\'d need to know \'bout escape with \\ that do \n newlines and \t tabs.'
poem = """
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explanation
\n\t\twhere there is none.
"""
print "-------------"
print poem
print "-------------"
five = 10 - 2 + 3 - 6
print "This should be five: %s" % five
def secret_formula(started):
jelly_beans = started * 500
jars = jelly_beans / 1000
crates = jars / 100
return jelly_beans, jars, crates
start_point = 10000
beans, jars, crates = secret_formula(start_point)
print "With a starting point of : %d" % start_point
print "We'd have %d beans, %d jars, and %d crates." % (beans, jars, crates)
start_point = start_point / 10
print "We can also do that this way:"
print "We'd have %d beans, %d jars, and %d crates." % secret_formula(start_point)
2 个解决方案
#1
Does that mean the variable is not valid after the
return
?这是否意味着该变量在返回后无效?
Yes; when the function ends all locally-scoped names (jelly_beans
, in your example) cease to exist. The name jelly_beans
is only accessible within secret_formula
.
是;当函数结束时,所有本地范围的名称(在您的示例中为jelly_beans)都不再存在。名称jelly_beans只能在secret_formula中访问。
It seems like after the indentation of the function, I can't print the variable used in the function part.
似乎在函数缩进之后,我无法打印函数部分中使用的变量。
You cannot access them from outside the function, even via the function name (so neither jelly_beans
nor secret_formula.jelly_beans
give you access to the value). This is actually a good thing, as it means that you can encapsulate the internal logic within the function without exposing it to the rest of your program.
您无法从函数外部访问它们,即使是通过函数名称也是如此(因此,jelly_beans和secret_formula.jelly_beans都不允许您访问该值)。这实际上是一件好事,因为这意味着您可以将内部逻辑封装在函数中,而不会将其暴露给程序的其余部分。
From the answer it says "When you return it then it can be assigned to a variable for later".
从答案中可以看出“当你返回它时,它可以被分配给一个变量供以后使用”。
Only the local names inside the function are deleted, not necessarily the objects they reference. When you return jelly_beans, jars, crates
, this passes the objects (not the names) back to whatever called secret_formula
. You can give the objects the same names outside the function or something completely different:
只删除函数内的本地名称,不一定是它们引用的对象。当你返回jelly_beans,jar,crates时,它会将对象(不是名称)传递回任何名为secret_formula的东西。您可以在函数外部为对象指定相同的名称或完全不同的对象:
foo, bar, baz = secret_formula(...)
This article is a useful introduction to how naming works in Python.
本文是有关如何在Python中使用命名的有用介绍。
#2
Due to the scoping rules of Python, the name jelly_beans
is valid only inside the secret_formula
function. That is the reason you can not refer to it via a statement like print jelly_beans
outside the function.
由于Python的作用域规则,名称jelly_beans仅在secret_formula函数内有效。这就是你不能通过函数之外的print jelly_beans之类的语句引用它的原因。
Notice that secret_formula
returns a tuple to its caller. Therefore, when you type:
请注意,secret_formula将一个元组返回给其调用者。因此,当您键入:
beans, jars, crates = secret_formula(start_point)
you specify a call to secret_formula
(with a certain parameter), and assign the contents of the tuple to three different names.
指定对secret_formula(具有特定参数)的调用,并将元组的内容分配给三个不同的名称。
- the returned value of
jelly_beans
is assigned tobeans
- the returned value of
jars
is assigned tojars
- the returned value of
crates
is assigned tocrates
jelly_beans的返回值分配给bean
将jar的返回值分配给jar
将包装箱的返回值分配给包装箱
In the latter two cases, it is important to note that even if the names are the same, the underlying objects may not be (not due to scoping rules, however - see the comments).
在后两种情况下,重要的是要注意即使名称相同,底层对象也可能不是(不是由于范围规则,但是 - 请参阅注释)。
#1
Does that mean the variable is not valid after the
return
?这是否意味着该变量在返回后无效?
Yes; when the function ends all locally-scoped names (jelly_beans
, in your example) cease to exist. The name jelly_beans
is only accessible within secret_formula
.
是;当函数结束时,所有本地范围的名称(在您的示例中为jelly_beans)都不再存在。名称jelly_beans只能在secret_formula中访问。
It seems like after the indentation of the function, I can't print the variable used in the function part.
似乎在函数缩进之后,我无法打印函数部分中使用的变量。
You cannot access them from outside the function, even via the function name (so neither jelly_beans
nor secret_formula.jelly_beans
give you access to the value). This is actually a good thing, as it means that you can encapsulate the internal logic within the function without exposing it to the rest of your program.
您无法从函数外部访问它们,即使是通过函数名称也是如此(因此,jelly_beans和secret_formula.jelly_beans都不允许您访问该值)。这实际上是一件好事,因为这意味着您可以将内部逻辑封装在函数中,而不会将其暴露给程序的其余部分。
From the answer it says "When you return it then it can be assigned to a variable for later".
从答案中可以看出“当你返回它时,它可以被分配给一个变量供以后使用”。
Only the local names inside the function are deleted, not necessarily the objects they reference. When you return jelly_beans, jars, crates
, this passes the objects (not the names) back to whatever called secret_formula
. You can give the objects the same names outside the function or something completely different:
只删除函数内的本地名称,不一定是它们引用的对象。当你返回jelly_beans,jar,crates时,它会将对象(不是名称)传递回任何名为secret_formula的东西。您可以在函数外部为对象指定相同的名称或完全不同的对象:
foo, bar, baz = secret_formula(...)
This article is a useful introduction to how naming works in Python.
本文是有关如何在Python中使用命名的有用介绍。
#2
Due to the scoping rules of Python, the name jelly_beans
is valid only inside the secret_formula
function. That is the reason you can not refer to it via a statement like print jelly_beans
outside the function.
由于Python的作用域规则,名称jelly_beans仅在secret_formula函数内有效。这就是你不能通过函数之外的print jelly_beans之类的语句引用它的原因。
Notice that secret_formula
returns a tuple to its caller. Therefore, when you type:
请注意,secret_formula将一个元组返回给其调用者。因此,当您键入:
beans, jars, crates = secret_formula(start_point)
you specify a call to secret_formula
(with a certain parameter), and assign the contents of the tuple to three different names.
指定对secret_formula(具有特定参数)的调用,并将元组的内容分配给三个不同的名称。
- the returned value of
jelly_beans
is assigned tobeans
- the returned value of
jars
is assigned tojars
- the returned value of
crates
is assigned tocrates
jelly_beans的返回值分配给bean
将jar的返回值分配给jar
将包装箱的返回值分配给包装箱
In the latter two cases, it is important to note that even if the names are the same, the underlying objects may not be (not due to scoping rules, however - see the comments).
在后两种情况下,重要的是要注意即使名称相同,底层对象也可能不是(不是由于范围规则,但是 - 请参阅注释)。