python:全局变量的正确使用
这是代码!
import csv
def do_work():
global data
global b
get_file()
samples_subset1()
return
def get_file():
start_file='thefile.csv'
with open(start_file, 'rb') as f:
data = list(csv.reader(f))
import collections
counter = collections.defaultdict(int)
for row in data:
counter[row[10]] += 1
return
def samples_subset1():
with open('/pythonwork/samples_subset1.csv', 'wb') as outfile:
writer = csv.writer(outfile)
sample_cutoff=5000
b_counter=0
global b
b=[]
for row in data:
if counter[row[10]] >= sample_cutoff:
global b
b.append(row)
writer.writerow(row)
#print b[b_counter]
b_counter+=1
return
我是Python的初学者。我的代码运行的方式是我调用 do_work , do_Work 将调用其他函数。这是我的问题:
如果我需要仅由 2 个函数看到
数据
,我应该将其设为全局吗?如果不是那么我应该如何调用samples_subset1
?我应该从get_file
还是从do_work
调用它?代码可以工作,但是您能指出有关其编写方式的其他优点/缺点吗?
我正在处理 csv 文件,有多个步骤。我将这些步骤分解为不同的函数,例如
get_file
、samples_subset1
,并且我还会添加更多函数。我应该继续按照现在的方式进行操作吗?
根据以下答案之一,这是新代码:
import csv
import collections
def do_work():
global b
(data,counter)=get_file('thefile.csv')
samples_subset1(data, counter,'/pythonwork/samples_subset1.csv')
return
def get_file(start_file):
with open(start_file, 'rb') as f:
global data
data = list(csv.reader(f))
counter = collections.defaultdict(int)
for row in data:
counter[row[10]] += 1
return (data,counter)
def samples_subset1(data,counter,output_file):
with open(output_file, 'wb') as outfile:
writer = csv.writer(outfile)
sample_cutoff=5000
b_counter=0
global b
b=[]
for row in data:
if counter[row[10]] >= sample_cutoff:
global b
b.append(row)
writer.writerow(row)
#print b[b_counter]
b_counter+=1
return
here's the code!
import csv
def do_work():
global data
global b
get_file()
samples_subset1()
return
def get_file():
start_file='thefile.csv'
with open(start_file, 'rb') as f:
data = list(csv.reader(f))
import collections
counter = collections.defaultdict(int)
for row in data:
counter[row[10]] += 1
return
def samples_subset1():
with open('/pythonwork/samples_subset1.csv', 'wb') as outfile:
writer = csv.writer(outfile)
sample_cutoff=5000
b_counter=0
global b
b=[]
for row in data:
if counter[row[10]] >= sample_cutoff:
global b
b.append(row)
writer.writerow(row)
#print b[b_counter]
b_counter+=1
return
i am a beginner at python. the way my code runs is i call do_work and do_Work will call the other functions. here are my questions:
if i need
data
to be seen by only 2 functions should i make it global? if not then how should i callsamples_subset1
? should i call it fromget_file
or fromdo_work
?the code works but can you please point other good/bad things about the way it is written?
i am processing a csv file and there are multiple steps. i am breaking down the steps into different functions like
get_file
,samples_subset1
, and there are more that i will add. should i continue to do it the way i am doing it right now here i call each individual function fromdo_work
?
here is the new code, according to one of the answers below:
import csv
import collections
def do_work():
global b
(data,counter)=get_file('thefile.csv')
samples_subset1(data, counter,'/pythonwork/samples_subset1.csv')
return
def get_file(start_file):
with open(start_file, 'rb') as f:
global data
data = list(csv.reader(f))
counter = collections.defaultdict(int)
for row in data:
counter[row[10]] += 1
return (data,counter)
def samples_subset1(data,counter,output_file):
with open(output_file, 'wb') as outfile:
writer = csv.writer(outfile)
sample_cutoff=5000
b_counter=0
global b
b=[]
for row in data:
if counter[row[10]] >= sample_cutoff:
global b
b.append(row)
writer.writerow(row)
#print b[b_counter]
b_counter+=1
return
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据经验,避免全局变量。
在这里,这很容易:
让 get_file 返回数据
那么你可以说
另外,我会在文件顶部进行所有导入
As a rule of thumb, avoid global variables.
Here, it's easy:
let get_file return data
then you can say
Also, I'd do all the imports on the top of the file
如果您必须使用全局(有时我们必须),您可以以Pythonic方式定义它,并只允许某些模块访问它,而无需在顶部使用令人讨厌的
global
关键字你所有的函数/类。创建一个仅包含全局数据的新模块(在您的情况下,假设为
csvGlobals.py
):然后您想要访问此数据的每个文件都可以按以下方式执行此操作:
if you must use a global (and sometimes we must) you can define it in a Pythonic way and give only certain modules access to it without the nasty
global
keyword at the top of all of your functions/classes.Create a new module containing only global data (in your case let's say
csvGlobals.py
):and then each file you want to have access to this data can do so in this fashion:
如果您想在两个或多个函数之间共享数据,那么通常最好使用类并将函数转换为方法,将全局变量转换为类实例上的属性。
顺便说一句,您不需要在每个函数末尾都有 return 语句。仅当您想要返回值或在函数中间返回时,才需要显式返回。
If you want to share data between two or more functions then it is generally better to use a class and turn the functions into methods and the global variable into attributes on the class instance.
BTW, you do not need the return statement at the end of every function. You only need to explicitly return if you want to either return a value or to return in the middle of the function.