From c0487c7d65ba7a9e5b8124bce694bb78ccb56e38 Mon Sep 17 00:00:00 2001 From: Valonia Kim <34000495+Valoniakim@users.noreply.github.com> Date: Thu, 19 Apr 2018 21:32:16 +0800 Subject: [PATCH 1/6] 20180206 How to start an open source program in your company.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 翻译完毕 --- ... an open source program in your company.md | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 translated/talk/20180206 How to start an open source program in your company.md diff --git a/translated/talk/20180206 How to start an open source program in your company.md b/translated/talk/20180206 How to start an open source program in your company.md new file mode 100644 index 0000000000..74d9c4c873 --- /dev/null +++ b/translated/talk/20180206 How to start an open source program in your company.md @@ -0,0 +1,47 @@ +如何在企业中开展开源项目 +====== + +![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/LAW_openisopen.png?itok=FjmDxIaL) + +很多互联网企业如 Google, Facebook, Twitter 等,都已经正式建立了数个开源项目(有的公司中建立了单独的开源项目部门)。在这个特定的项目内,开源项目的成本和成果都由公司内部消化。在这样一个实际的部门中,企业可以清晰透明地执行开源策略,这是企业成功开源化的一个必要过程。开源项目部门的职责包括:制定使用、分配、选择和审查代码的相关政策;培训开发技术人员和服从法律法规。 + +互联网企业并不是唯一一种运行开源项目的企业,有调查发现产业中 [65% 的企业][1]的运营都与开源相关。在过去几年中 [VMware][2], [Amazon][3], [Microsoft][4] 等企业,甚至连[英国政府][5]都开始聘用开源相关人员,开展开源项目。可见近年来商业领域乃至政府都十分重视开源策略,在这样的环境下,各界也需要跟上他们的步伐,建立开源项目。 + +### 怎样建立开源项目 + +虽然根据企业的需求不同,各开源部门会有特殊的调整,但下面几个基本步骤是建立每个开源部门都会经历的,它们是: + + * **选定一位领导者:** 选出一位合适的领导之是建立开源项目的第一步。 [TODO Group][6] 发布了一份[开源人员基础工作任务清单][7],你可以根据这个清单筛选人员。 + * **确定项目构架:** 开源项目可以根据其服务的企业类型改变侧重点,来适应不同种类的企业需求,以在各类企业中成功运行。知识型企业可以把开源项目放在法律事务部运行,技术驱动型企业可以把开源项目放在着眼于提高企业效能的部门中,如工程部。其他类型的企业可以把开源项目放在市场部内运行,以此促进开源产品的销售。TODO Group 发布的[开源项目案例][8]或许可以给你些启发。 + * **制定规章制度:** 开源策略的实施需要有一套规章制度,其中应当具体列出企业成员进行开源工作的标准流程,来减少失误的发生。这个流程应当简洁明了且简单易行,最好可以用设备进行自动监察。如果工作人员有质疑标准流程的热情和能力,并提出改进意见,那再好不过了。许多活跃在开源领域的企业中,Google 和 TODO 发布的规章制度十分值得借鉴。你可以参照 [Google 发布的制度][9]起草适用于自己企业的规章制度,用 [TODO 的规章制度][10]进行参考。 +  +### 建立开源项目是企业发展中的关键一步 + +建立开源项目部门对很多企业来说是关键一步,尤其是对于那些软件公司或是想要转型进入软件领域的公司。不论雇员的满意度或是开发效率上,在开源项目中企业可以获得巨大的利益,这些利益远远大于对开源项目需要的长期投资。在开源之路上有很多资源可以帮助你成功,例如 TODO Group 的[《怎样创建开源项目》][11],[《开源项目的价值评估》][12]和[《管理开源项目的几种工具》][13]都很适合初学者阅读。 + +随着越来越多的企业形成开源项目,开源社区自身的可持续性逐渐加强,这会对这些企业的开源项目产生积极影响,促进企业的发展,这是企业和开源间的良性循环。我希望以上这些信息能够帮到你,祝你在建立开源项目的路上一路顺风。 + +-------------------------------------------------------------------------------- + +via: https://opensource.com/article/18/1/how-start-open-source-program-your-company + +作者:[Chris Aniszczyk][a] +译者:[Valoniakim](https://github.com/Valoniakim) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]:https://opensource.com/users/caniszczyk +[1]:https://www.blackducksoftware.com/2016-future-of-open-source +[2]:http://www.cio.com/article/3095843/open-source-tools/vmware-today-has-a-strong-investment-in-open-source-dirk-hohndel.html +[3]:http://fortune.com/2016/12/01/amazon-open-source-guru/ +[4]:https://opensource.microsoft.com/ +[5]:https://www.linkedin.com/jobs/view/169669924 +[6]:http://todogroup.org +[7]:https://github.com/todogroup/job-descriptions +[8]:https://github.com/todogroup/guides/tree/master/casestudies +[9]:https://opensource.google.com/docs/why/ +[10]:https://github.com/todogroup/policies +[11]:https://github.com/todogroup/guides/blob/master/creating-an-open-source-program.md +[12]:https://github.com/todogroup/guides/blob/master/measuring-your-open-source-program.md +[13]:https://github.com/todogroup/guides/blob/master/tools-for-managing-open-source-programs.md From 7b71d5988cbd17524a449269ae142386071331f2 Mon Sep 17 00:00:00 2001 From: "Xingyu.Wang" Date: Thu, 19 Apr 2018 21:33:15 +0800 Subject: [PATCH 2/6] PRF:20180110 8 simple ways to promote team communication.md @Valoniakim --- ...mple ways to promote team communication.md | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/translated/talk/20180110 8 simple ways to promote team communication.md b/translated/talk/20180110 8 simple ways to promote team communication.md index cf417b2e5b..0be4c5b53a 100644 --- a/translated/talk/20180110 8 simple ways to promote team communication.md +++ b/translated/talk/20180110 8 simple ways to promote team communication.md @@ -1,50 +1,48 @@ 八种敏捷团队的提升方法 ====== + +> 在这个列表中,没有项目管理软件,这里不包含清单,也没有与 GitHub 整合,只是几种组织思维和提高团队交流的方法。 + ![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/world_remote_teams.png?itok=Wk1yBFv6) 图片来源:opensource.com -你也许经常听说下面这句话:工具很多,时间很少。为了节约您的时间,我列出了几款我最常用的提高敏捷团队工作效率的工具。如果你也是一名敏捷主义者,你可能听说过类似的工具,但我这里提到的仅限于开源工具。 +你也许经常听说下面这句话:工具太多,时间太少。为了节约您的时间,我列出了几款我最常用的提高敏捷团队工作效率的工具。如果你也是一名敏捷主义者,你可能听说过类似的工具,但我这里提到的仅限于开源工具。 -**请注意!** 这些工具和你想象的可能有点不同。它们并不是项目管理软件——这领域已经有一篇[好文章][1]了。因此不包含清单,也没有与 GitHub 整合,只是几种组织思维和提高团队交流的方法。 +**请注意!** 这些工具和你想象的可能有点不同。它们并不是项目管理软件——这领域已经有一篇[好文章][1]了。因此这里不包含清单,也没有与 GitHub 整合,只是几种组织思维和提高团队交流的方法。 ### 组建一个充满积极反馈的团队 -如果在产业中大部分人都习惯了输出、接收负面消息,就很难有人对同事进行正面反馈的输出。这并不奇怪,同事中有人沉浸在赞美中,就会有人在不停地说“干得漂亮!”“没有你我们很难完成任务。”但是赞美并不使人痛苦,它通常能激励大家更好地为团队工作。下面两个软件可以帮助你向同事表达赞扬。 - - * 对开发团队来说,[Management 3.0][2] 是有着大量[免费资源][3]的珍宝,可以尽情使用。其中 Feedback Wraps 的观念最引人注目(不仅仅是因为它让我们联想到墨西哥卷)。 [Feedback Wraps][4] 是一个经过六步对用户进行反馈的程序,也许你会认为它是为了负面消息设计的,但我们发现它在表达积极评论方面十分有效。 - * [Happiness Packets][5] 为用户在开源社区内提供匿名正面反馈服务。它尤其适合不太习惯人际交往的用户或是不知道说什么好的情况。Happiness Packets 拥有一份[公开的评论档案][6](这些评论都已经得到授权),你可以浏览大家的评论,从中得到灵感,对别人做出暖心的称赞。它还有个特殊功能,能够屏蔽负面消息。 - +如果在产业中大部分人都习惯了输出、接收负面消息,就很难有人对同事进行正面反馈的输出。这并不奇怪,当人们乐于给出赞美时,人们就会竞相向别人说“干得漂亮!”、“没有你我们很难完成任务。”赞美别人干的漂亮并不会使你痛苦,它通常能激励大家更好地为团队工作。下面两个软件可以帮助你向同事表达赞扬。 +* 对开发团队来说,[Management 3.0][2] 是个有着大量[免费资源][3]的宝箱,可以尽情使用。其中 Feedback Wraps 的观念最引人注目(不仅仅是因为它让我们联想到墨西哥卷)。 [Feedback Wraps][4] 是一个经过六步对用户进行反馈的程序,也许你会认为它是为了负面反馈而设计的,但我们发现它在表达积极评论方面十分有效。 +* [Happiness Packets][5] 为用户提供了在开源社区内匿名正面反馈的服务。它尤其适合不太习惯人际交往的用户或是不知道说什么好的情况。Happiness Packets 拥有一份[公开的评论档案][6](这些评论都已经得到授权分享),你可以浏览大家的评论,从中得到灵感,对别人做出暖心的称赞。它还有个特殊功能,能够屏蔽恶意消息。 ### 思考工作的意义 这很难定义。在敏捷领域中,成功的关键包括定义人物角色和产品愿景,还要向整个敏捷团队说明此项工作的意义。产品开发人员和项目负责人能够获得的开源工具数量极为有限,对于这一点我们有些失望。 -在 Rat Hat 中,最受尊敬也最为常用于训练敏捷团队的开源工具之一是Product Vision Board 。它出自产品管理专家 Roman Pichler 之手,Roman Pichler 提供了[大量工具和模版][7]来帮助敏捷团队理解他们工作的意义。(你需要提供电子邮箱地址才能下载这些工具。) - - * [Product Vision Board][8] 的模版通过简单但有效的问题引导团队转变思考方式,将思考工作的意义置于具体工作方法之前。 - * 我们也很喜欢 Roman 的 [Product Management Test][9],它能够通过简便快捷的网页表单,引领团队重新定义产品开发人员的角色,并且找出程序漏洞。我们推荐产品开发团队周期性地完成此项测试,重新分析失败原因。 - +在 Rat Hat 公司,最受尊敬也最为常用于训练敏捷团队的开源工具之一是 Product Vision Board 。它出自产品管理专家 Roman Pichler 之手,Roman Pichler 提供了[大量工具和模版][7]来帮助敏捷团队理解他们工作的意义。(你需要提供电子邮箱地址才能下载这些工具。) +* [Product Vision Board][8] 的模版通过简单但有效的问题引导团队转变思考方式,将思考工作的意义置于具体工作方法之前。 +* 我们也很喜欢 Roman 的 [Product Management Test][9],它能够通过简便快捷的网页表单,引领团队重新定义产品开发人员的角色,并且找出程序漏洞。我们推荐产品开发团队周期性地完成此项测试,重新分析失败原因。 ### 对工作内容的直观化 你是否曾为一个大案子焦头烂额,连熟悉的步骤也在脑海中乱成一团?我们也遇到过这种情况。使用思维导图可以梳理你脑海中的想法,使其直观化。你不需要一下就想出整件事该怎么进行,你只需要你的头脑,一块白板(或者是思维导图软件)和一些思考的时间。 - * 在这个领域中我们最喜欢的开源工具是 [Xmind3][10]。它支持多种平台运行(Linux, MacOS, 和 Windows),以便与他人共享文件。如果你对工具的要求很高,推荐使用 [updated version][11],提供电子邮箱地址即可免费下载使用。 - * 如果你很看重灵活性,Eduard Lucena 在Fedora Magazine 中提供的 [three additional options][12] 就十分适合。你可以在 Fedora 杂志上找到这些软件的获取方式,其他信息可以在它们的项目页找到。 +* 在这个领域中我们最喜欢的开源工具是 [Xmind3][10]。它支持多种平台运行(Linux、MacOS 和 Windows),以便与他人共享文件。如果你对工具的要求很高,推荐使用[其更新版本][11],提供电子邮箱地址即可免费下载使用。 +* 如果你很看重灵活性,Eduard Lucena 在 Fedora Magazine 中提供的 [三个附加选项][12] 就十分适合。你可以在 Fedora 杂志上找到这些软件的获取方式,其他信息可以在它们的项目页找到。 * [Labyrinth][13] * [View Your Mind][14] * [FreeMind][15] - - 像我们开头说的一样,提高敏捷团队工作效率的工具有很多,如果你有特别喜欢的相关开源工具,请在评论中与大家分享。 ### 作者简介 -Jen Krieger :Red Hat 的首席敏捷架构师,在软件开发领域已经工作超过20年,曾在瀑布及敏捷生命周期等领域扮演多种角色。目前在 Red Hat 负责针对 CI/CD 最佳效果的部际 DevOps 活动。最近她在与 Project Atomic & OpenShift 团队合作。她最近在引领公司向着敏捷团队改革,并增加开源项目在公司内的认知度。 + +Jen Krieger :Red Hat 的首席敏捷架构师,在软件开发领域已经工作超过 20 年,曾在瀑布及敏捷生命周期等领域扮演多种角色。目前在 Red Hat 负责针对 CI/CD 最佳效果的部际 DevOps 活动。最近她在与 Project Atomic & OpenShift 团队合作。她最近在引领公司向着敏捷团队改革,并增加开源项目在公司内的认知度。 -------------------------------------------------------------------------------- @@ -52,7 +50,7 @@ via: https://opensource.com/article/18/1/foss-tools-agile-teams 作者:[Jen Krieger][a] 译者:[Valoniakim](https://github.com/Valoniakim) -校对:[校对者ID](https://github.com/校对者ID) +校对:[wxy](https://github.com/wxy) 本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 From 7b830a4805c3e533d9257845d015b7a02246de2d Mon Sep 17 00:00:00 2001 From: Valonia Kim <34000495+Valoniakim@users.noreply.github.com> Date: Thu, 19 Apr 2018 21:33:27 +0800 Subject: [PATCH 3/6] Delete 20180206 How to start an open source program in your company.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 翻译完毕 --- ... an open source program in your company.md | 51 ------------------- 1 file changed, 51 deletions(-) delete mode 100644 sources/talk/20180206 How to start an open source program in your company.md diff --git a/sources/talk/20180206 How to start an open source program in your company.md b/sources/talk/20180206 How to start an open source program in your company.md deleted file mode 100644 index b654cd5d07..0000000000 --- a/sources/talk/20180206 How to start an open source program in your company.md +++ /dev/null @@ -1,51 +0,0 @@ -Translating by valoniakim - -How to start an open source program in your company -====== - -![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/LAW_openisopen.png?itok=FjmDxIaL) - -Many internet-scale companies, including Google, Facebook, and Twitter, have established formal open source programs (sometimes referred to as open source program offices, or OSPOs for short), a designated place where open source consumption and production is supported inside a company. With such an office in place, any business can execute its open source strategies in clear terms, giving the company tools needed to make open source a success. An open source program office's responsibilities may include establishing policies for code use, distribution, selection, and auditing; engaging with open source communities; training developers; and ensuring legal compliance. - -Internet-scale companies aren't the only ones establishing open source programs; studies show that [65% of companies][1] across industries are using and contributing to open source. In the last couple of years we’ve seen [VMware][2], [Amazon][3], [Microsoft][4], and even the [UK government][5] hire open source leaders and/or create open source programs. Having an open source strategy has become critical for businesses and even governments, and all organizations should be following in their footsteps. - -### How to start an open source program - -Although each open source office will be customized to a specific organization’s needs, there are standard steps that every company goes through. These include: - - * **Finding a leader:** Identifying the right person to lead the open source program is the first step. The [TODO Group][6] maintains a list of [sample job descriptions][7] that may be helpful in finding candidates. - * **Deciding on the program structure:** There are a variety of ways to fit an open source program office into an organization's existing structure, depending on its focus. Companies with large intellectual property portfolios may be most comfortable placing the office within the legal department. Engineering-driven organizations may choose to place the office in an engineering department, especially if the focus of the office is to improve developer productivity. Others may want the office to be within the marketing department to support sales of open source products. For inspiration, the TODO Group offers [open source program case studies][8] that can be useful. - * **Setting policies and processes:** There needs to be a standardized method for implementing the organization’s open source strategy. The policies, which should require as little oversight as possible, lay out the requirements and rules for working with open source across the organization. They should be clearly defined, easily accessible, and even automated with tooling. Ideally, employees should be able to question policies and provide recommendations for improving or revising them. Numerous organizations active in open source, such as Google, [publish their policies publicly][9], which can be a good place to start. The TODO Group offers examples of other [open source policies][10] organizations can use as resources. - - - -### A worthy step - -Opening an open source program office is a big step for most organizations, especially if they are (or are transitioning into) a software company. The benefits to the organization are tremendous and will more than make up for the investment in the long run—not only in employee satisfaction but also in developer efficiency. There are many resources to help on the journey. The TODO Group guides [How to Create an Open Source Program][11], [Measuring Your Open Source Program's Success][12], and [Tools for Managing Open Source Programs][13] are great starting points. - -Open source will truly be sustainable as more companies formalize programs to contribute back to these projects. I hope these resources are useful to you, and I wish you luck on your open source program journey. - --------------------------------------------------------------------------------- - -via: https://opensource.com/article/18/1/how-start-open-source-program-your-company - -作者:[Chris Aniszczyk][a] -译者:[译者ID](https://github.com/译者ID) -校对:[校对者ID](https://github.com/校对者ID) - -本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 - -[a]:https://opensource.com/users/caniszczyk -[1]:https://www.blackducksoftware.com/2016-future-of-open-source -[2]:http://www.cio.com/article/3095843/open-source-tools/vmware-today-has-a-strong-investment-in-open-source-dirk-hohndel.html -[3]:http://fortune.com/2016/12/01/amazon-open-source-guru/ -[4]:https://opensource.microsoft.com/ -[5]:https://www.linkedin.com/jobs/view/169669924 -[6]:http://todogroup.org -[7]:https://github.com/todogroup/job-descriptions -[8]:https://github.com/todogroup/guides/tree/master/casestudies -[9]:https://opensource.google.com/docs/why/ -[10]:https://github.com/todogroup/policies -[11]:https://github.com/todogroup/guides/blob/master/creating-an-open-source-program.md -[12]:https://github.com/todogroup/guides/blob/master/measuring-your-open-source-program.md -[13]:https://github.com/todogroup/guides/blob/master/tools-for-managing-open-source-programs.md From 813b2544fb264e8e23228302554b06276d62300e Mon Sep 17 00:00:00 2001 From: "Xingyu.Wang" Date: Thu, 19 Apr 2018 21:34:35 +0800 Subject: [PATCH 4/6] PUB: 20180110 8 simple ways to promote team communication.md @Valoniakim https://linux.cn/article-9560-1.html --- .../20180110 8 simple ways to promote team communication.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {translated/talk => published}/20180110 8 simple ways to promote team communication.md (100%) diff --git a/translated/talk/20180110 8 simple ways to promote team communication.md b/published/20180110 8 simple ways to promote team communication.md similarity index 100% rename from translated/talk/20180110 8 simple ways to promote team communication.md rename to published/20180110 8 simple ways to promote team communication.md From 4cf0f701dffe540375bfc58fde3d18c2c676f369 Mon Sep 17 00:00:00 2001 From: "Xingyu.Wang" Date: Thu, 19 Apr 2018 21:45:35 +0800 Subject: [PATCH 5/6] PRF:20180207 Python Global Keyword (With Examples).md @MjSeven --- ...7 Python Global Keyword (With Examples).md | 152 ++++++++++-------- 1 file changed, 87 insertions(+), 65 deletions(-) diff --git a/translated/tech/20180207 Python Global Keyword (With Examples).md b/translated/tech/20180207 Python Global Keyword (With Examples).md index 981de74983..1a15db8e44 100644 --- a/translated/tech/20180207 Python Global Keyword (With Examples).md +++ b/translated/tech/20180207 Python Global Keyword (With Examples).md @@ -1,12 +1,14 @@ -Python Global 关键字(含示例) +初识 Python: global 关键字 ====== -在读这篇文章之前,确保你对 [Python Global,Local 和 Nonlocal 变量][1] 有一定的基础。 + +在读这篇文章之前,确保你对 [Python 全局、本地和非本地变量][1] 有一定的基础。 ### global 关键字简介 在 Python 中,`global` 关键字允许你修改当前范围之外的变量。它用于创建全局变量并在本地上下文中更改变量。 ### global 关键字的规则 + 在 Python 中,有关 `global` 关键字基本规则如下: * 当我们在一个函数中创建一个变量时,默认情况下它是本地变量。 @@ -14,63 +16,72 @@ Python Global 关键字(含示例) * 我们使用 `global` 关键字在一个函数中来读写全局变量。 * 在一个函数外使用 `global` 关键字没有效果。 -#### 使用 global 关键字(含示例) +### 使用 global 关键字(含示例) 我们来举个例子。 -##### 示例 1:从函数内部访问全局变量 +#### 示例 1:从函数内部访问全局变量 - c = 1 # 全局变量 +``` +c = 1 # 全局变量 +def add(): + print(c) - def add(): - print(c) - - add() +add() +``` 运行程序,输出为: - 1 +``` +1 +``` 但是我们可能有一些场景需要从函数内部修改全局变量。 -##### 示例 2:在函数内部修改全局变量 +#### 示例 2:在函数内部修改全局变量 - c = 1 # 全局变量 - - def add(): - c = c + 2 # 将 c 增加 2 - print(c) - - add() +``` +c = 1 # 全局变量 +def add(): + c = c + 2 # 将 c 增加 2 + print(c) +add() +``` 运行程序,输出显示错误: - UnboundLocalError: local variable 'c' referenced before assignment +``` +UnboundLocalError: local variable 'c' referenced before assignment +``` 这是因为在函数中,我们只能访问全局变量但是不能修改它。 解决的办法是使用 `global` 关键字。 -##### 示例 3:使用 global 在函数中改变全局变量 +#### 示例 3:使用 global 在函数中改变全局变量 - c = 0 # global variable +``` +c = 0 # global variable - def add(): - global c - c = c + 2 # 将 c 增加 2 - print("Inside add():", c) - - add() - print("In main:", c) +def add(): + global c + c = c + 2 # 将 c 增加 2 + print("Inside add():", c) + +add() +print("In main:", c) +``` 运行程序,输出为: - Inside add(): 2 - In main: 2 +``` +Inside add(): 2 +In main: 2 +``` -在上面的程序中,我们在 `add()` 函数中定义了 c 将其作为 global 关键字。 +在上面的程序中,我们在 `add()` 函数中定义了 `c` 将其作为全局关键字。 -然后,我们给变量 c 增加 `1`,(译注:这里应该是给 c 增加 `2` )即 `c = c + 2`。之后,我们调用了 `add()` 函数。最后,打印全局变量 c。 +然后,我们给变量 `c` 增加 `2`,即 `c = c + 2`。之后,我们调用了 `add()` 函数。最后,打印全局变量 `c`。 正如我们所看到的,在函数外的全局变量也发生了变化,`c = 2`。 @@ -80,68 +91,79 @@ Python Global 关键字(含示例) 以下是如何通过 Python 模块共享全局变量。 -##### 示例 4:在Python模块中共享全局变量 +#### 示例 4:在Python模块中共享全局变量 创建 `config.py` 文件来存储全局变量 - a = 0 - b = "empty" +``` +a = 0 +b = "empty" +``` 创建 `update.py` 文件来改变全局变量 - import config +``` +import config - config.a = 10 - config.b = "alphabet" +config.a = 10 +config.b = "alphabet" +``` 创建 `main.py` 文件来测试其值的变化 - import config - import update +``` +import config +import update - print(config.a) - print(config.b) +print(config.a) +print(config.b) +``` 运行 `main.py`,输出为: - 10 - alphabet +``` +10 +alphabet +``` 在上面,我们创建了三个文件: `config.py`, `update.py` 和 `main.py`。 -在 `config.py` 模块中保存了全局变量 a 和 b。在 `update.py` 文件中,我们导入了 `config.py` 模块并改变了 a 和 b 的值。同样,在 `main.py` 文件,我们导入了 `config.py` 和 `update.py` 模块。最后,我们打印并测试全局变量的值,无论它们是否被改变。 +在 `config.py` 模块中保存了全局变量 `a` 和 `b`。在 `update.py` 文件中,我们导入了 `config.py` 模块并改变了 `a` 和 `b` 的值。同样,在 `main.py` 文件,我们导入了 `config.py` 和 `update.py` 模块。最后,我们打印并测试全局变量的值,无论它们是否被改变。 ### 在嵌套函数中的全局变量 以下是如何在嵌套函数中使用全局变量。 -##### 示例 5:在嵌套函数中使用全局变量 +#### 示例 5:在嵌套函数中使用全局变量 - def foo(): - x = 20 - - def bar(): - global x - x = 25 +``` +def foo(): + x = 20 + def bar(): + global x + x = 25 - print("Before calling bar: ", x) - print("Calling bar now") - bar() - print("After calling bar: ", x) + print("Before calling bar: ", x) + print("Calling bar now") + bar() + print("After calling bar: ", x) - foo() - print("x in main : ", x) +foo() +print("x in main : ", x) +``` 输出为: - Before calling bar: 20 - Calling bar now - After calling bar: 20 - x in main : 25 +``` +Before calling bar: 20 +Calling bar now +After calling bar: 20 +x in main : 25 +``` -在上面的程序中,我们在一个嵌套函数 `bar()` 中声明了全局变量。在 `foo()` 函数中, 变量 x 没有全局关键字的作用。 +在上面的程序中,我们在一个嵌套函数 `bar()` 中声明了全局变量。在 `foo()` 函数中, 变量 `x` 没有全局关键字的作用。 -调用 `bar()` 之前和之后, 变量 x 取本地变量的值,即 `x = 20`。在 `foo()` 函数之外,变量 x 会取在函数 `bar()` 中的值,即 `x = 25`。这是因为在 `bar()` 中,我们对 x 使用 `global` 关键字创建了一个全局变量(本地范围)。 +调用 `bar()` 之前和之后, 变量 `x` 取本地变量的值,即 `x = 20`。在 `foo()` 函数之外,变量 `x` 会取在函数 `bar()` 中的值,即 `x = 25`。这是因为在 `bar()` 中,我们对 `x` 使用 `global` 关键字创建了一个全局变量(本地范围)。 如果我们在 `bar()` 函数内进行了任何修改,那么这些修改就会出现在本地范围之外,即 `foo()`。 @@ -151,7 +173,7 @@ via: [https://www.programiz.com/python-programming/global-keyword](https://www.p 作者:[programiz][a] 译者:[MjSeven](https://github.com/MjSeven) -校对:[校对者ID](https://github.com/校对者ID) +校对:[wxy](https://github.com/wxy) 本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 From bd065e18c09e1d329d2655a99c0727820d6657e7 Mon Sep 17 00:00:00 2001 From: "Xingyu.Wang" Date: Thu, 19 Apr 2018 21:46:03 +0800 Subject: [PATCH 6/6] PUB:20180207 Python Global Keyword (With Examples).md @MjSeven https://linux.cn/article-9561-1.html --- .../20180207 Python Global Keyword (With Examples).md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {translated/tech => published}/20180207 Python Global Keyword (With Examples).md (100%) diff --git a/translated/tech/20180207 Python Global Keyword (With Examples).md b/published/20180207 Python Global Keyword (With Examples).md similarity index 100% rename from translated/tech/20180207 Python Global Keyword (With Examples).md rename to published/20180207 Python Global Keyword (With Examples).md