python:全局变量的正确使用

发布于 2024-09-12 11:58:14 字数 2285 浏览 3 评论 0原文

这是代码!

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

  1. 如果我需要仅由 2 个函数看到数据,我应该将其设为全局吗?如果不是那么我应该如何调用samples_subset1?我应该从get_file还是从do_work调用它?

  2. 代码可以工作,但是您能指出有关其编写方式的其他优点/缺点吗?

  3. 我正在处理 csv 文件,有多个步骤。我将这些步骤分解为不同的函数,例如 get_filesamples_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:

  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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

〆凄凉。 2024-09-19 11:58:14

根据经验,避免全局变量。

在这里,这很容易:
让 get_file 返回数据
那么你可以说

data = get_file()
samples_subset1(data)

另外,我会在文件顶部进行所有导入

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

盗琴音 2024-09-19 11:58:14

如果您必须使用全局(有时我们必须),您可以以Pythonic方式定义它,并只允许某些模块访问它,而无需在顶部使用令人讨厌的global关键字你所有的函数/类。

创建一个仅包含全局数据的新模块(在您的情况下,假设为 csvGlobals.py):

# create an instance of some data you want to share across modules
data=[]

然后您想要访问此数据的每个文件都可以按以下方式执行此操作:

import csvGlobals

csvGlobals.data = [1,2,3,4]
for i in csvGlobals.data:
    print i

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):

# create an instance of some data you want to share across modules
data=[]

and then each file you want to have access to this data can do so in this fashion:

import csvGlobals

csvGlobals.data = [1,2,3,4]
for i in csvGlobals.data:
    print i
转角预定愛 2024-09-19 11:58:14

如果您想在两个或多个函数之间共享数据,那么通常最好使用类并将函数转换为方法,将全局变量转换为类实例上的属性。

顺便说一句,您不需要在每个函数末尾都有 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文