Earlier today I asked a question about passing dictionary values to a function. While I understand now how to accomplish what I was trying to accomplish the why question (which was not asked) was never answered. So my follow up is why can't I
今天早些时候我问了一个关于将字典值传递给函数的问题。虽然我现在明白如何完成我想要完成的事情但为什么问题(没有被问到)从未得到回答。所以我的跟进就是为什么我不能
def myFunction(newDict['bubba']):
some code to process the parameter
Is it simply because the parser rules do not allow this? I Googled for +Python +function +"allowable parameters" and did not find anything useful so I would appreciate any information.
是因为解析器规则不允许这样做吗?我用Google搜索+ Python +函数+“允许参数”,但没有找到任何有用的信息,所以我很感激任何信息。
I am oversimplifying what is going on. I have a dictionary that is structured like
我过于简单化了。我有一个结构相似的字典
myDict={outerkey1:(innerkey1:value,innerkey2:value,. . .innerkeyn:value),outerkey2:(innerkey1:value,innerkey2:value,. . .innerkeyn:value),. . .}
As I said, I know how to do what I wanted-I got a very helpful answer. But I started wondering why
正如我所说,我知道如何做我想做的事 - 我得到了一个非常有用的答案。但我开始想知道为什么
def myFunction(outerkey,myDict[outerkey]):
gives a syntax error which finely it occurred to me that it has to be a parsing problem.
给出了一个语法错误,我发现它必须是一个解析问题。
3 个解决方案
#1
Yes, the parser will reject this code.
是的,解析器将拒绝此代码。
Parameter lists are used in function definitions to bind identifiers within the function to arguments that are passed in from the outside on invocation.
参数列表用于函数定义,以将函数内的标识符绑定到在调用时从外部传入的参数。
Since newDict['bubba']
is not a valid identifier, this doesn't make any sense -- you need to provide it as an invocation argument instead of a function parameter, since function parameters can only be identifiers.
由于newDict ['bubba']不是有效的标识符,因此没有任何意义 - 您需要将其作为调用参数而不是函数参数提供,因为函数参数只能是标识符。
Because you seem interested in the formal grammar, here are the relevant parts:
因为您似乎对正式语法感兴趣,所以这里是相关部分:
funcdef ::=
[decorators] "def" funcname "(" [parameter_list] ")"
":" suite
parameter_list ::=
(defparameter ",")*
(~~"*" identifier [, "**" identifier]
| "**" identifier
| defparameter [","] )
defparameter ::=
parameter ["=" expression]
identifier ::=
(letter|"_") (letter | digit | "_")*
In fact, the construct you are trying to use as an identifier is a subscription:
实际上,您尝试用作标识符的构造是订阅:
subscription ::=
primary "[" expression_list "]"
#2
It looks like you might be confused between the definition of a function and calling that function. As a simple example:
看起来您可能会在函数的定义和调用该函数之间感到困惑。举个简单的例子:
def f(x):
y = x * x
print "x squared is", y
By itself, this function doesn't do anything (ie. it doesn't print anything). If you put this in a source file and run it, nothing will be output. But then if you add:
就其本身而言,此功能不起作用(即,它不会打印任何内容)。如果将其放在源文件中并运行它,则不会输出任何内容。但是如果你添加:
f(5)
you will get the output "x squared is 25". When Python encounters the name of the function followed by actual parameters, it substitutes the value(s) of those actual parameters for the formal parameter(s) in the function definition. In this case, the formal parameter is called x
. Then Python runs the function f
with the value of x
as 5. Now, the big benefit is you can use the same function again with a different value:
你会得到输出“x平方是25”。当Python遇到函数的名称后跟实际参数时,它会将这些实际参数的值替换为函数定义中的形式参数。在这种情况下,形式参数称为x。然后Python运行函数f,其值为x为5.现在,最大的好处是您可以使用不同的值再次使用相同的函数:
f(10)
prints "x squared is 100".
打印“x平方为100”。
I hope I've understood the source of your difficulty correctly.
我希望我能正确理解你的困难来源。
#3
You can only pass references to objects.
您只能传递对象的引用。
a = { 'a' : 123, 'b' : [ 1, 2, 3 ] }
def f(dictionary_value):
dictionary_value.append(4)
f( a['b'] )
Does what you expect. Dictionary element a['b']
is passed "by reference" to the function f
which updates the dictionary element.
你的期望吗?字典元素a ['b']通过“引用”传递给更新字典元素的函数f。
#1
Yes, the parser will reject this code.
是的,解析器将拒绝此代码。
Parameter lists are used in function definitions to bind identifiers within the function to arguments that are passed in from the outside on invocation.
参数列表用于函数定义,以将函数内的标识符绑定到在调用时从外部传入的参数。
Since newDict['bubba']
is not a valid identifier, this doesn't make any sense -- you need to provide it as an invocation argument instead of a function parameter, since function parameters can only be identifiers.
由于newDict ['bubba']不是有效的标识符,因此没有任何意义 - 您需要将其作为调用参数而不是函数参数提供,因为函数参数只能是标识符。
Because you seem interested in the formal grammar, here are the relevant parts:
因为您似乎对正式语法感兴趣,所以这里是相关部分:
funcdef ::=
[decorators] "def" funcname "(" [parameter_list] ")"
":" suite
parameter_list ::=
(defparameter ",")*
(~~"*" identifier [, "**" identifier]
| "**" identifier
| defparameter [","] )
defparameter ::=
parameter ["=" expression]
identifier ::=
(letter|"_") (letter | digit | "_")*
In fact, the construct you are trying to use as an identifier is a subscription:
实际上,您尝试用作标识符的构造是订阅:
subscription ::=
primary "[" expression_list "]"
#2
It looks like you might be confused between the definition of a function and calling that function. As a simple example:
看起来您可能会在函数的定义和调用该函数之间感到困惑。举个简单的例子:
def f(x):
y = x * x
print "x squared is", y
By itself, this function doesn't do anything (ie. it doesn't print anything). If you put this in a source file and run it, nothing will be output. But then if you add:
就其本身而言,此功能不起作用(即,它不会打印任何内容)。如果将其放在源文件中并运行它,则不会输出任何内容。但是如果你添加:
f(5)
you will get the output "x squared is 25". When Python encounters the name of the function followed by actual parameters, it substitutes the value(s) of those actual parameters for the formal parameter(s) in the function definition. In this case, the formal parameter is called x
. Then Python runs the function f
with the value of x
as 5. Now, the big benefit is you can use the same function again with a different value:
你会得到输出“x平方是25”。当Python遇到函数的名称后跟实际参数时,它会将这些实际参数的值替换为函数定义中的形式参数。在这种情况下,形式参数称为x。然后Python运行函数f,其值为x为5.现在,最大的好处是您可以使用不同的值再次使用相同的函数:
f(10)
prints "x squared is 100".
打印“x平方为100”。
I hope I've understood the source of your difficulty correctly.
我希望我能正确理解你的困难来源。
#3
You can only pass references to objects.
您只能传递对象的引用。
a = { 'a' : 123, 'b' : [ 1, 2, 3 ] }
def f(dictionary_value):
dictionary_value.append(4)
f( a['b'] )
Does what you expect. Dictionary element a['b']
is passed "by reference" to the function f
which updates the dictionary element.
你的期望吗?字典元素a ['b']通过“引用”传递给更新字典元素的函数f。