简体   繁体   English

关于python中字典和函数的初学者问题

[英]beginner question about dictionaries and function in python

I would like to get some help to understand the following code for "Higher or Lower game reproduction from one of the course I am doing right know.我想从我正在做的一门课程中获得一些帮助来理解以下“更高或更低的游戏再现”代码。

For the second line of code where the function is defined as : account_name=account["name"] I do not understand why they named as "account" where they referring to the key value "name" .对于函数定义为: account_name=account["name"]的第二行代码,我不明白为什么它们命名为 "account" ,其中它们指的是键值"name" When I learned functions, I thought the name where it says "account" has to say "data", because that is the name of the list that we going to use to get the info.当我学习函数时,我认为它说“帐户”的名称必须说“数据”,因为这是我们将用来获取信息的列表的名称。 Whilst we going to define a new variable like account_name=data["name"] .同时我们将定义一个新变量,如account_name=data["name"] But looks like I am wrong.但看起来我错了。 The other thing is why we passing a value to the function "format data" which is nowhere In the code as "account".另一件事是为什么我们将一个值传递给函数“格式化数据”,该值在代码中无处作为“帐户”。

So would like to know how the function gets all the necessary information when the list name is completely different, how it is getting the value.所以想知道当列表名称完全不同时,函数如何获取所有必要的信息,它是如何获取值的。

Every advice is appreciated.每一个建议都值得赞赏。

from game_data import data

#function that converts the data to a certain format to display

def format_data(account):

    account_name=account["name"]
  
    account_follower=account["follower_count"]
  
    account_desc=account["description"]
  
    account_country=account["country"]
  
    return f"{account_name} {account_follower} {account_desc} {account_country}"


#randomly chooses one of the dictionaries from the list

account_a=random.choice(data)

account_b=random.choice(data)

if account_a==account_b:
 
    account_b=random.choice(data)

print(f"compare A: {format_data(account_a)}")
print(f"compare B: {format_data(account_b)}")

My guess is that given the line: from game_data import data this makes the variable data available, which is a list .我的猜测是,鉴于以下行: from game_data import data这使得变量data可用,即list

The line account_a=random.choice(data) , then randomly picks one of the items.account_a=random.choice(data) ,然后随机选择其中一项。

The line: print(f"compare A: {format_data(account_a)}") then calls the format_data() function sending it the parameter account_a .行: print(f"compare A: {format_data(account_a)}")然后调用format_data()函数向它发送参数account_a

Now the format_data() function runs for the first time with parameter account which refers to the same object that account_a refers to.现在format_data()函数第一次使用参数account运行,该参数引用了account_a引用的同一对象。

All this function does it to pick out some elements of the dict and reformats them into a string which is returned.所有这些函数都是为了挑选dict一些元素并将它们重新格式化为返回的字符串。

This formatted string then gets printed by the above: print(f"compare A: {format_data(account_a)}")这个格式化的字符串然后被上面print(f"compare A: {format_data(account_a)}")出来: print(f"compare A: {format_data(account_a)}")


The thing you get confused about is calling functions and the names of parameters.您感到困惑的是调用函数和参数名称。

Where there is the call: format_data(account_a) , python simply does an assignment of:哪里有调用: format_data(account_a) ,python 只是简单地分配:

account = account_a as part of the call and the function format_data() now has the parameter account . account = account_a作为调用的一部分,函数format_data()现在具有参数account

In the example you have given, the line:在您给出的示例中,该行:

def format_data(account):

Tells us that there is a method which takes a parameter, which in this instance is called "account".告诉我们有一个方法接受一个参数,在这个例子中被称为“帐户”。 The parameter could be named anything at all (with some exceptions), such as "variable", "foo" or "x".参数可以命名为任何名称(有一些例外),例如“variable”、“foo”或“x”。 However it is useful to give it a meaningful name, and in this case they have gone for "account".然而,给它一个有意义的名字是很有用的,在这种情况下,他们已经使用了“帐户”。 This name "account" is used throughout the function and it acts on whatever has been passed in by whatever called the function in the first place.这个名称“帐户”在整个函数中使用,它作用于首先调用该函数的任何传入的任何内容。

It is worth noting that the variable "account" is only in scope for that function.值得注意的是,变量“account”仅在该函数的作用域内。 If you tried to access "account" outside of the function you would get an error.如果您尝试在函数之外访问“帐户”,则会出现错误。 This is where the idea of variable scope comes in, and you can read about this more if you search online.这就是变量作用域的概念出现的地方,如果您在线搜索,您可以阅读更多相关信息。

If you chose to use the variable name "data" for the function it would be possible, but actually could be more confusing as then there would be two variables, both called "data" but with different scopes.如果您选择为函数使用变量名称“data”,那将是可能的,但实际上可能会更令人困惑,因为会有两个变量,都称为“data”但具有不同的范围。 One is in scope for the function call, and one for the script.一个在函数调用的范围内,一个在脚本的范围内。 So it is best to not use variable names in that way, and instead choose different names to avoid confusion and accidental mistakes.所以最好不要这样使用变量名,而是选择不同的名称,以避免混淆和意外错误。


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM