Python 函数和变量 - 学习 Python 困难的方法 ex19

Python Functions and Variables - Learn Python The Hard way ex19

这个函数不是从脚本中调用变量吗?这与 Zed Shaw 所说的背道而驰...

我把评论放在自己里面了,我的问题是这样的:

Zed Shaw 说:

The variables in a function are not connected to variables in a script

但据我所知,该函数在顶部带有缩进,然后当缩进开始时它会创建变量,然后将这些变量链接到该函数。

这个函数不是从脚本中调用变量吗?

我找不到合适的答案:有人可以详细说明这段脚本并告诉我我是否看错了吗?

# This is the function with declared variables inside
def cheese_and_crackers(cheese_count, boxes_of_crackers):
    print  "You have %d cheeses!" % cheese_count
    print "You have %d boxes of crackers!"  % boxes_of_crackers
    print "Man thats enough for a party!"
    print "Get a Blanket. \n"

# This declares the amounts in the functions variables
print "We can just give the function numbers directly:"
cheese_and_crackers (20, 30)

# This variable sets the amounts
print "OR, we can use variables from our script:"
amount_of_cheese = 10
amount_of_crackers = 50
#This variable combines the two above and stores it in a single variable for the function to call
cheese_and_crackers(amount_of_cheese, amount_of_crackers)

# This uses the function, but has predefined variables with maths
print "We can even do math inside too:"
cheese_and_crackers(10 + 20, 5 + 6)
#This combines variables with maths
print "And we can combine the two, variables and math:"
cheese_and_crackers (amount_of_cheese + 100, amount_of_crackers + 1000)

除了作为参数传递给函数之外,变量在任何时候都不会连接到函数参数。调用 foo(bar) 不会将 bar 连接到 foo(),它只是将 bar 的值作为函数的第一个参数传递。如果该参数也恰好被称为 "bar" 那么这是巧合。