python:正确使用全局变量 [英] python: proper usage of global variable

查看:202
本文介绍了python:正确使用全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的代码!

  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将调用其他函数。这里是我的问题:


  1. 如果我需要 data 只有2个功能应该让它全局?如果不是那么我应该如何调用 samples_subset1 ?我应该从 get_file 或从 do_work 吗?


  2. >

    代码工作,但你能指出其他好的/坏的东西关于它写的方式吗?


  3. 我正在处理一个csv文件,是多个步骤。我把这些步骤分解成不同的功能,如 get_file samples_subset1 ,还有更多的,我会添加。我应该继续做我的方式,我现在这样做现在在这里我调用每个单独的函数从 do_work




    1. 这里是新代码,根据以下答案之一:

        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:

      1. if i need datato be seen by only 2 functions should i make it global? if not then how should i call samples_subset1? should i call it from get_file or from do_work?

      2. the code works but can you please point other good/bad things about the way it is written?

      3. 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 from do_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
      

      解决方案

      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屋!

      查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆