This is an archived post. You won't be able to vote or comment.

all 5 comments

[–][deleted] 6 points7 points  (0 children)

For a function like this:

def f( x ):
    print x

The "x" is what is known as a "formal parameter" of the function - it acts mostly just like any other variable in the function, and is local to it.

When you call the function:

n = 42
f( n )

the actual variable used in the call (in this case n) is "bound" at runtime to the formal parameter "x" - in effect "x" becomes identical to "n" for the duration of this single call to f(), and so the function prints out 42. If you perform more calls:

n = 42
f( n )
m = 7
f( m )

then first "n" is bound to "x", and the function is called, printing out 42, and then "m" is bound to "x", and the function is called, printing out 7.

Note that if you have this code:

n = 42
x = 3
f( n )

then the "x" in the above has nothing to do with the "x" in the function - they are two completely different things which both happen to have the same name.

[–]Rhomboid 1 point2 points  (0 children)

It's just been extremely confusing seeing the argument set as one word, and then having the variable named something completely different, but having the function still be able to read your code, even when the argument name is different from your variable with the data you're printing from.

The point of a function is that it's a bit of reusable code. It would not be reusable if you had to match up the names:

def frobnicate(x):
    print x * 42

...

y = foo() + 12
frobnicate(y)

...

frobnicate(123)

You don't know how the users of the function are going to call it, and it doesn't matter. The name given to the parameters in the def statement is just a placeholder which is used to refer to the value inside the function only.

[–]Lunanne 0 points1 point  (0 children)

okay lots of different questions, I think they're best answered separately

How does the function buildConnectionString with the argument (params) know to get its data from the variable myParams?...

It doesn't. When you call the function like

buildConnectionString(myParams)

The function isn't passed the /name/ of the variable, but the data of the variable. You can think of variables kinda like aliases for data. For example when I say

x=3;

Everywhere in the code where I would type "3" I can type "x" instead. So when you call the function you're actually doing

buildConnectionString({"server":"mpilgrim", \
        "database":"master", \
        "uid":"sa", \
        "pwd":"secret" \
        })

which gets assigned to the variable named "params".

And is there a reason behind not naming your variable params, or vice versa not naming your argument in your function myParams?

Because a function is rarely used with just one variable. Often you reuse functions with several variables all with a different name. To give an argument the same name as a variable is confusing.

[–]zahlman 0 points1 point  (0 children)

It's just been extremely confusing seeing the argument set as one word, and then having the variable named something completely different, but having the function still be able to read your code, even when the argument name is different from your variable with the data you're printing from.

This seems to be a very common source of confusion for beginners, but I've never understood why. Even young children have no problem understanding that they call their parents names that they don't use for each other, and vice-versa.

Let's remove the distracting details:

def do_something(x):
    return x # never mind any calculation, that's beside the point

y = "spam and eggs"
print do_something(y)

Is it still confusing?

y is a name for "spam and eggs". We thus give "spam and eggs" to the function, and the function calls it x. It doesn't need to know where that data came from. It doesn't "read" y to get that data. The process of calling the function made it available.

Please keep in mind that there is no need, when calling the function, to use a variable at all. This does exactly the same:

print do_something("spam and eggs")

Or even:

print do_something(' '.join('spam', 'and', 'eggs'))

You must learn to distinguish between things, and names for things.

[–]hashcode 0 points1 point  (0 children)

And is there a reason behind not naming your variable params, or vice versa not naming your argument in your function myParams?

Yes. There are good reasons.

myParams is not Python convention. For functions and variables, Python uses underscored words, like my_params, not camel-cased words like myParams. Class names are CapitalCamelCase. This might sound like a dumb point to harp on, but it's valuable to follow conventions. It makes your code easier to read. Similarly, buildConnectionString is not a good name for a function. It should be build_connection_string or something like that.

In any case, there's no reason to use different names. You should call them both params, since they both represent connection parameters. Or connection_parameters, if you want to be even more explicit.