Why can't I pass a direct reference to the dictionary value of the function?

Earlier today I asked a question about passing dictionary values ​​to a function. Although now I understand how to accomplish what I was trying to solve, why the question (which was not asked) was never answered. So my observation is why I can't

def myFunction(newDict['bubba']):
    some code to process the parameter

      

Is it just because the parser rules don't allow it? I Googled for + Python + function + "valid parameters" and didn't find anything useful, so I would appreciate any information.

I'm oversimplifying what's 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 to wonder why

 def myFunction(outerkey,myDict[outerkey]):

      

gives a syntax error which I thought was due to the fact that it must be a parsing problem.

+1


a source to share


3 answers


Yes, the analyzer will reject this code.

Parameter lists are used in function definitions to bind identifiers within a function to arguments that are passed externally when called .

Since it is newDict['bubba']

not a valid identifier, it doesn't make any sense - you need to provide it as a call argument instead of a function parameter, since function parameters can only be identifiers.

Since you seem to be interested in 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 "]"

      

+6


a source


It looks like you might be confused between defining 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 does nothing (i.e. prints nothing). If you put this in your source file and run it, nothing will be output. But if you add:

f(5)

      



you will get the output "x squared is 25". When Python encounters a function name followed by actual parameters, it replaces 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

. Python then runs the function f

with the value x

5. Now the big advantage is that you can use the same function again with a different value:

f(10)

      

prints "x squared is 100".

I hope I understand correctly the source of your difficulties.

+2


a source


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. The dictionary item a['b']

is passed "by reference" to the function f

that updates the dictionary item.

+2


a source







All Articles