python:正确使用全局变量 [英] python: proper usage of global variable
问题描述
这里的代码!
import csv
def do_work():
全局数据
全局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)
数据行:
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
全局b
b = []
对于数据中的行:
如果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将调用其他函数。这里是我的问题:
-
如果我需要
data
只有2个功能应该让它全局?如果不是那么我应该如何调用samples_subset1
?我应该从get_file
或从do_work
吗? -
我正在处理一个csv文件,是多个步骤。我把这些步骤分解成不同的功能,如
get_file
,samples_subset1
,还有更多的,我会添加。我应该继续做我的方式,我现在这样做现在在这里我调用每个单独的函数从do_work
? 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
?
>
代码工作,但你能指出其他好的/坏的东西关于它写的方式吗?
这里是新代码,根据以下答案之一:
import csv
import collections
def do_work():
global b
(data,counter)= get_file('thefile.csv')
sample_subset1(data,counter,'/ pythonwork / samples_subset1.csv')
return
def get_file(start_file):
with open(start_file,'rb' )as f:
全局数据
data = list(csv.reader(f))
counter = collections.defaultdict(int)
数据中的行:
counter [row [10]] + = 1
return(data,counter)
def samples_subset1(data,counter,output_file):
与打开(output_file,'wb')作为outfile:
writer = csv.writer(outfile)
sample_cutoff = 5000
b_counter = 0
全局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
在这里,很容易:
let get_file返回数据
那么你可以说
data = get_file()
samples_subset1(data)
此外,我会执行文件顶部的所有导入
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:
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
As a rule of thumb, avoid global variables.
Here, it's easy: let get_file return data then you can say
data = get_file()
samples_subset1(data)
Also, I'd do all the imports on the top of the file
这篇关于python:正确使用全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!