[美好的一天,我想知道是否可以从下面的函数附加到全局声明的变量(列表),这是我正在使用的逻辑结构。 client.number的结果将打印数字,但是当打印var a时,我会收到一个空列表或[]。这是因为全局变量在函数外部声明或分配了var a = [],还是不能从函数内部附加全局变量?我认为可能正在发生的事情是它对var a进行了引用并将其附加在函数中,但是它更多的是只能在函数中包含该值的指针Im不能完全确定它是如何工作的]
global a
a = []
def build_list():
for client in contact_list:
if client.number is not in a:
a.apend(client.number)
print(a)
所以错误是我对全局变量如何工作的理解。我的印象是您在函数外部全局声明了它,因此函数可以访问数据,而实际上您是在函数内部全局声明的,以便函数可以读取其中包含的数据。我声明了全局变量,将代码附加到列表的工作代码如下所示#!/usr/bin/python
import subprocess
try:
import skill.python as forum_hero
except ImportError:
subprocess.call("pip install more_python_knowledge")
try:
import argparse
except ImportError:
subprocess.call("pip install wife_for_arguments")
``a=[]
b=["tim","john","steve","larry","Brandon","Brenton ","Liam","Noah","William","James","Oliver","Benjamin","Elijah","Lucas","Mason","Logan","Alexander","Ethan","Michael","Daniel","Henry","Jackson","Sebastian","Aiden","Matthew","Samuel","David","Joseph","Carter","Owen","Wyatt","John","Jack","Luke","Jayden","Dylan","Grayson","Levi","Isaac","Gabriel","Julian","Mateo","Anthony","Jaxon","Lincoln","Joshua","Christopher","Andrew","Theodore","Caleb"]
def build_list():
for name in b:
if name not in a:
a.append(name)
`def call_list():`
` global a`
` for name in a:`
` print(name)`
build_list()
call_list()
print(a.__str__() + ": End of second print")