标签归档:global-variables

我可以使用__init__.py定义全局变量吗?

问题:我可以使用__init__.py定义全局变量吗?

我想定义一个常量,该常量应在包的所有子模块中可用。我以为最好的地方__init__.py在根包的文件中。但是我不知道该怎么做。假设我有几个子包,每个子包都有几个模块。如何从这些模块访问该变量?

当然,如果这是完全错误的,并且有更好的选择,我想知道。

I want to define a constant that should be available in all of the submodules of a package. I’ve thought that the best place would be in in the __init__.py file of the root package. But I don’t know how to do this. Suppose I have a few subpackages and each with several modules. How can I access that variable from these modules?

Of course, if this is totally wrong, and there is a better alternative, I’d like to know it.


回答 0

您应该能够将它们放入__init__.py。这一直都在做。

mypackage/__init__.py

MY_CONSTANT = 42

mypackage/mymodule.py

from mypackage import MY_CONSTANT
print "my constant is", MY_CONSTANT

然后,导入mymodule:

>>> from mypackage import mymodule
my constant is 42

不过,如果您确实有常量,将它们放在单独的模块(constants.py,config.py,…)中,然后将其放入包命名空间中是合理的(可能是最佳做法),然后导入他们。

mypackage/__init__.py

from mypackage.constants import *

尽管如此,这并不会自动在包模块的命名空间中包含常量。包中的每个模块仍然必须从mypackage或从中显式导入常量mypackage.constants

You should be able to put them in __init__.py. This is done all the time.

mypackage/__init__.py:

MY_CONSTANT = 42

mypackage/mymodule.py:

from mypackage import MY_CONSTANT
print "my constant is", MY_CONSTANT

Then, import mymodule:

>>> from mypackage import mymodule
my constant is 42

Still, if you do have constants, it would be reasonable (best practices, probably) to put them in a separate module (constants.py, config.py, …) and then if you want them in the package namespace, import them.

mypackage/__init__.py:

from mypackage.constants import *

Still, this doesn’t automatically include the constants in the namespaces of the package modules. Each of the modules in the package will still have to import constants explicitly either from mypackage or from mypackage.constants.


回答 1

你不能这样做。您必须将常量明确地导入每个模块的命名空间中。实现此目的的最佳方法是在“ config”模块中定义常量,并将其导入所需的任何位置:

# mypackage/config.py
MY_CONST = 17

# mypackage/main.py
from mypackage.config import *

You cannot do that. You will have to explicitely import your constants into each individual module’s namespace. The best way to achieve this is to define your constants in a “config” module and import it everywhere you require it:

# mypackage/config.py
MY_CONST = 17

# mypackage/main.py
from mypackage.config import *

回答 2

您可以在任何地方定义全局变量,但这是一个非常糟糕的主意。导入__builtin__模块并修改或向该模块添加属性,突然间您有了新的内置常量或函数。实际上,当我的应用程序安装gettext时,我在所有模块中都获得了_()函数,而无需导入任何内容。因此这是可能的,但当然仅适用于应用程序类型的项目,而不适用于可重用的包或模块。

而且我想没人会推荐这种做法。命名空间有什么问题?说应用程序版本模块,这样我可以有像“全局”变量version.VERSIONversion.PACKAGE_NAME等等。

You can define global variables from anywhere, but it is a really bad idea. import the __builtin__ module and modify or add attributes to this modules, and suddenly you have new builtin constants or functions. In fact, when my application installs gettext, I get the _() function in all my modules, without importing anything. So this is possible, but of course only for Application-type projects, not for reusable packages or modules.

And I guess no one would recommend this practice anyway. What’s wrong with a namespace? Said application has the version module, so that I have “global” variables available like version.VERSION, version.PACKAGE_NAME etc.


回答 3

只是想补充一点,可以使用config.ini文件使用常量,并使用configparser库在脚本中对其进行解析。这样,您可以在多种情况下使用常量。例如,如果您有两个单独的url请求的参数常量,只需将它们标记为:

mymodule/config.ini
[request0]
conn = 'admin@localhost'
pass = 'admin'
...

[request1]
conn = 'barney@localhost'
pass = 'dinosaur'
...

我发现Python网站上的文档非常有帮助。我不确定Python 2和3之间是否有任何区别,因此这是两者的链接:

对于Python 3:https//docs.python.org/3/library/configparser.html#module-configparser

对于Python 2:https//docs.python.org/2/library/configparser.html#module-configparser

Just wanted to add that constants can be employed using a config.ini file and parsed in the script using the configparser library. This way you could have constants for multiple circumstances. For instance if you had parameter constants for two separate url requests just label them like so:

mymodule/config.ini
[request0]
conn = 'admin@localhost'
pass = 'admin'
...

[request1]
conn = 'barney@localhost'
pass = 'dinosaur'
...

I found the documentation on the Python website very helpful. I am not sure if there are any differences between Python 2 and 3 so here are the links to both:

For Python 3: https://docs.python.org/3/library/configparser.html#module-configparser

For Python 2: https://docs.python.org/2/library/configparser.html#module-configparser


为什么全局变量是邪恶的?[关闭]

问题:为什么全局变量是邪恶的?[关闭]

我试图找出为什么global在Python(以及一般编程)中将使用视为不好的做法。有人可以解释吗?具有更多信息的链接也将不胜感激。

I’m trying to find out why the use of global is considered to be bad practice in python (and in programming in general). Can somebody explain? Links with more info would also be appreciated.


回答 0

这与Python无关。全局变量在任何编程语言中都是不好的。

但是,全局常量在概念上与全局变量并不相同。全局常数完全无害。在Python中,两者之间的区别纯属约定:CONSTANTS_ARE_CAPITALIZEDglobals_are_not

全局变量之所以不好是因为它们使函数具有隐藏的(非显而易见的,令人惊讶的,难以检测的,难以诊断的)副作用,从而导致复杂性的增加,并有可能导致产生Spaghetti代码

但是,即使在函数式编程中,也可以合理使用全局状态(局部状态和可变性也是如此),无论是算法优化,降低复杂性,缓存和记忆化,还是移植以命令式代码库为基础的结构的实用性。

总而言之,您的问题可以通过多种方式回答,因此您最好的选择就是使用Google“为什么全局变量不好”。一些例子:

如果您想更深入地了解造成副作用的原因以及许多其他启发性的内容,则应该学习函数式编程:

This has nothing to do with Python; global variables are bad in any programming language.

However, global constants are not conceptually the same as global variables; global constants are perfectly harmless. In Python the distinction between the two is purely by convention: CONSTANTS_ARE_CAPITALIZED and globals_are_not.

The reason global variables are bad is that they enable functions to have hidden (non-obvious, surprising, hard to detect, hard to diagnose) side effects, leading to an increase in complexity, potentially leading to Spaghetti code.

However, sane use of global state is acceptable (as is local state and mutability) even in functional programming, either for algorithm optimization, reduced complexity, caching and memoization, or the practicality of porting structures originating in a predominantly imperative codebase.

All in all, your question can be answered in many ways, so your best bet is to just google “why are global variables bad”. Some examples:

If you want to go deeper and find out why side effects are all about, and many other enlightening things, you should learn Functional Programming:


回答 1

是的,从理论上讲,全局变量(通常是“状态”)是邪恶的。在实践中,如果查看python的packages目录,您会发现那里的大多数模块都是以一堆全局声明开头的。显然,人们对此没有任何问题。

特别是对于python,全局变量的可见性仅限于一个模块,因此没有影响整个程序的“真实”全局变量-使其危害程度降低。还有一点:没有const,所以当您需要一个常量时,必须使用一个全局变量。

在我的实践中,如果碰巧在函数中修改了全局变量,那么global即使在技术上没有必要,我也总是用声明它,例如:

cache = {}

def foo(args):
    global cache

    cache[args] = ...

这使得全局变量的操作更易于跟踪。

Yes, in theory, globals (and “state” in general) are evil. In practice, if you look into your python’s packages directory you’ll find that most modules there start with a bunch of global declarations. Obviously, people have no problem with them.

Specifically to python, globals’ visibility is limited to a module, therefore there are no “true” globals that affect the whole program – that makes them a way less harmful. Another point: there are no const, so when you need a constant you have to use a global.

In my practice, if I happen to modify a global in a function, I always declare it with global, even if there technically no need for that, as in:

cache = {}

def foo(args):
    global cache

    cache[args] = ...

This makes globals’ manipulations easier to track down.


回答 2

关于该主题的个人观点是,在函数逻辑中使用全局变量意味着其他一些代码可以更改该函数的逻辑和预期输出,这将使调试非常困难(尤其是在大型项目中),并使测试更加困难也一样

此外,如果您考虑其他人(例如开源社区,同事等)阅读代码,他们将很难理解设置全局变量的位置,已更改的位置以及相对于此全局变量的期望隔离功能,可以通过读取功能定义本身来确定其功能。

(可能)违反纯函数定义

我相信干净且(几乎)没有错误的代码应具有尽可能纯净的功能(请参阅纯功能)。纯函数是具有以下条件的函数:

  1. 给定相同的参数值,该函数始终求值相同的结果值。函数结果值不能取决于在程序执行过程中或在程序的不同执行之间可能更改的任何隐藏信息或状态,也不能取决于I / O设备的任何外部输入(通常-参见下文)。
  2. 结果评估不会引起任何语义上可观察到的副作用或输出,例如可变对象的突变或输出到I / O设备。

全局变量违反了以上至少一项(如果不是全部),因为外部代码可能会导致意外的结果。

纯函数的另一个清晰定义:“纯函数是将其所有输入作为显式参数并将其所有输出作为显式结果的函数。” [1]。具有全局变量违反了纯函数的概念,因为未明确给出或返回输入或输出之一(全局变量)。

(可能)违反单元测试FIRST原则

另外对,如果你考虑的单元测试和第一原理(˚F AST测试, ndependent测试,[R epeatable,Ş精灵验证和牛逼 imely)可能会违反独立的测试原理(这意味着测试不依赖彼此)。

具有全局变量(并非总是如此),但在大多数情况下(至少是到目前为止我所看到的),是准备并将结果传递给其他函数。这也违反了该原理。如果以这种方式使用了全局变量(即必须先在函数Y中设置函数X中使用的全局变量),则意味着要对单元X进行单元测试,必须首先运行测试/运行函数Y。

全局常量

另一方面,正如其他人已经提到的那样,如果全局变量用作“常量”变量会更好一些,因为该语言不支持常量。但是,我总是更喜欢使用类并将“常量”作为类成员,而不使用全局变量。如果您有一个代码,两个不同的类需要共享一个全局变量,那么您可能需要重构您的解决方案并使您的类独立。

我不认为不应使用全局变量。但是,如果使用它们,那么作者应该考虑一些原则(上面可能提​​到的原则以及其他软件工程原则和良好实践),以获得更干净,几乎没有错误的代码。

A personal opinion on the topic is that having global variables being used in a function logic means that some other code can alter the logic and the expected output of that function which will make debugging very hard (especially in big projects) and will make testing harder as well.

Furthermore, if you consider other people reading your code (open-source community, colleagues etc) they will have a hard time trying to understand where the global variable is being set, where has been changed and what to expect from this global variable as opposed to an isolated function that its functionality can be determined by reading the function definition itself.

(Probably) Violating Pure Function definition

I believe that a clean and (nearly) bug-free code should have functions that are as pure as possible (see pure functions). A pure function is the one that has the following conditions:

  1. The function always evaluates the same result value given the same argument value(s). The function result value cannot depend on any hidden information or state that may change while program execution proceeds or between different executions of the program, nor can it depend on any external input from I/O devices (usually—see below).
  2. Evaluation of the result does not cause any semantically observable side effect or output, such as mutation of mutable objects or output to I/O devices.

Having global variables is violating at least one of the above if not both as an external code can probably cause unexpected results.

Another clear definition of pure functions: “Pure function is a function that takes all of its inputs as explicit arguments and produces all of its outputs as explicit results.” [1]. Having global variables violates the idea of pure functions since an input and maybe one of the outputs (the global variable) is not explicitly being given or returned.

(Probably) Violating Unit testing F.I.R.S.T principle

Further on that, if you consider unit-testing and the F.I.R.S.T principle (Fast tests, Independent tests, Repeatable, Self-Validating and Timely) will probably violate the Independent tests principle (which means that tests don’t depend on each other).

Having a global variable (not always) but in most of the cases (at least of what I have seen so far) is to prepare and pass results to other functions. This violates this principle as well. If the global variable has been used in that way (i.e the global variable used in function X has to be set in a function Y first) it means that to unit test function X you have to run test/run function Y first.

Globals as constants

On the other hand and as other people have already mentioned, if the global variable is used as a “constant” variable can be slightly better since the language does not support constants. However, I always prefer working with classes and having the “constants” as a class member and not use a global variable at all. If you have a code that two different classes require to share a global variable then you probably need to refactor your solution and make your classes independent.

I don’t believe that globals shouldn’t be used. But if they are used the authors should consider some principles (the ones mentioned above perhaps and other software engineering principles and good practices) for a cleaner and nearly bug-free code.


回答 3

它们是必不可少的,屏幕就是一个很好的例子。但是,在多线程环境中或在涉及许多开发人员的情况下,实际上常常会出现问题:谁(错误地)设置或清除了它?根据体系结构,分析可能很昂贵并且经常需要。虽然可以读取全局var,但是必须例如通过单线程或线程安全类来控制对其的写入。因此,全球变种人担心由于自身被认为是邪恶的后果而可能产生高昂的开发成本。因此,一般而言,最好将全局变量的数量保持在较低水平。

They are essential, the screen being a good example. However, in a multithreaded environment or with many developers involved, in practice often the question arises: who did (erraneously) set or clear it? Depending on the architecture, analysis can be costly and be required often. While reading the global var can be ok, writing to it must be controlled, for example by a single thread or threadsafe class. Hence, global vars arise the fear of high development costs possible by the consequences for which themselves are considered evil. Therefore in general, it’s good practice to keep the number of global vars low.


在config.py中提供全局配置变量的最Pythonic方法?[关闭]

问题:在config.py中提供全局配置变量的最Pythonic方法?[关闭]

在我对过度复杂的简单事物的无尽追求中,我正在研究最“ Pythonic”的方法来在Python egg包中的典型“ config.py ”中提供全局配置变量。

传统方式(啊,好吧,# define!)如下:

MYSQL_PORT = 3306
MYSQL_DATABASE = 'mydb'
MYSQL_DATABASE_TABLES = ['tb_users', 'tb_groups']

因此,以下列方式之一导入全局变量:

from config import *
dbname = MYSQL_DATABASE
for table in MYSQL_DATABASE_TABLES:
    print table

要么:

import config
dbname = config.MYSQL_DATABASE
assert(isinstance(config.MYSQL_PORT, int))

这是有道理的,但有时可能会有些混乱,尤其是在您要记住某些变量的名称时。此外,提供一个以变量为属性“配置”对象可能更灵活。因此,从bpython config.py文件开始,我想到了:

class Struct(object):

    def __init__(self, *args):
        self.__header__ = str(args[0]) if args else None

    def __repr__(self):
        if self.__header__ is None:
             return super(Struct, self).__repr__()
        return self.__header__

    def next(self):
        """ Fake iteration functionality.
        """
        raise StopIteration

    def __iter__(self):
        """ Fake iteration functionality.
        We skip magic attribues and Structs, and return the rest.
        """
        ks = self.__dict__.keys()
        for k in ks:
            if not k.startswith('__') and not isinstance(k, Struct):
                yield getattr(self, k)

    def __len__(self):
        """ Don't count magic attributes or Structs.
        """
        ks = self.__dict__.keys()
        return len([k for k in ks if not k.startswith('__')\
                    and not isinstance(k, Struct)])

和一个“ config.py”,该类导入该类,内容如下:

from _config import Struct as Section

mysql = Section("MySQL specific configuration")
mysql.user = 'root'
mysql.pass = 'secret'
mysql.host = 'localhost'
mysql.port = 3306
mysql.database = 'mydb'

mysql.tables = Section("Tables for 'mydb'")
mysql.tables.users = 'tb_users'
mysql.tables.groups =  'tb_groups'

并以这种方式使用:

from sqlalchemy import MetaData, Table
import config as CONFIG

assert(isinstance(CONFIG.mysql.port, int))

mdata = MetaData(
    "mysql://%s:%s@%s:%d/%s" % (
         CONFIG.mysql.user,
         CONFIG.mysql.pass,
         CONFIG.mysql.host,
         CONFIG.mysql.port,
         CONFIG.mysql.database,
     )
)

tables = []
for name in CONFIG.mysql.tables:
    tables.append(Table(name, mdata, autoload=True))

这似乎是在包内存储和获取全局变量的一种更具可读性,表现力和灵活性的方式。

有史以来最大的想法?应对这些情况的最佳实践是什么?什么是您的存储和获取全局名称和变量您的包内的方法吗?

In my endless quest in over-complicating simple stuff, I am researching the most ‘Pythonic’ way to provide global configuration variables inside the typical ‘config.py‘ found in Python egg packages.

The traditional way (aah, good ol’ #define!) is as follows:

MYSQL_PORT = 3306
MYSQL_DATABASE = 'mydb'
MYSQL_DATABASE_TABLES = ['tb_users', 'tb_groups']

Therefore global variables are imported in one of the following ways:

from config import *
dbname = MYSQL_DATABASE
for table in MYSQL_DATABASE_TABLES:
    print table

or:

import config
dbname = config.MYSQL_DATABASE
assert(isinstance(config.MYSQL_PORT, int))

It makes sense, but sometimes can be a little messy, especially when you’re trying to remember the names of certain variables. Besides, providing a ‘configuration’ object, with variables as attributes, might be more flexible. So, taking a lead from bpython config.py file, I came up with:

class Struct(object):

    def __init__(self, *args):
        self.__header__ = str(args[0]) if args else None

    def __repr__(self):
        if self.__header__ is None:
             return super(Struct, self).__repr__()
        return self.__header__

    def next(self):
        """ Fake iteration functionality.
        """
        raise StopIteration

    def __iter__(self):
        """ Fake iteration functionality.
        We skip magic attribues and Structs, and return the rest.
        """
        ks = self.__dict__.keys()
        for k in ks:
            if not k.startswith('__') and not isinstance(k, Struct):
                yield getattr(self, k)

    def __len__(self):
        """ Don't count magic attributes or Structs.
        """
        ks = self.__dict__.keys()
        return len([k for k in ks if not k.startswith('__')\
                    and not isinstance(k, Struct)])

and a ‘config.py’ that imports the class and reads as follows:

from _config import Struct as Section

mysql = Section("MySQL specific configuration")
mysql.user = 'root'
mysql.pass = 'secret'
mysql.host = 'localhost'
mysql.port = 3306
mysql.database = 'mydb'

mysql.tables = Section("Tables for 'mydb'")
mysql.tables.users = 'tb_users'
mysql.tables.groups =  'tb_groups'

and is used in this way:

from sqlalchemy import MetaData, Table
import config as CONFIG

assert(isinstance(CONFIG.mysql.port, int))

mdata = MetaData(
    "mysql://%s:%s@%s:%d/%s" % (
         CONFIG.mysql.user,
         CONFIG.mysql.pass,
         CONFIG.mysql.host,
         CONFIG.mysql.port,
         CONFIG.mysql.database,
     )
)

tables = []
for name in CONFIG.mysql.tables:
    tables.append(Table(name, mdata, autoload=True))

Which seems a more readable, expressive and flexible way of storing and fetching global variables inside a package.

Lamest idea ever? What is the best practice for coping with these situations? What is your way of storing and fetching global names and variables inside your package?


回答 0

我做了一次。最终,我发现简化的basicconfig.py可以满足我的需求。如果需要,您可以将命名空间与其他对象一起传递以供其引用。您还可以从代码中传递其他默认值。它还将属性和映射样式语法映射到同一配置对象。

I did that once. Ultimately I found my simplified basicconfig.py adequate for my needs. You can pass in a namespace with other objects for it to reference if you need to. You can also pass in additional defaults from your code. It also maps attribute and mapping style syntax to the same configuration object.


回答 1

只使用这样的内置类型怎么样:

config = {
    "mysql": {
        "user": "root",
        "pass": "secret",
        "tables": {
            "users": "tb_users"
        }
        # etc
    }
}

您可以按以下方式访问这些值:

config["mysql"]["tables"]["users"]

如果您愿意牺牲潜力在配置树中计算表达式,则可以使用YAML并得到一个更具可读性的配置文件,如下所示:

mysql:
  - user: root
  - pass: secret
  - tables:
    - users: tb_users

并使用PyYAML之类的库方便地解析和访问配置文件

How about just using the built-in types like this:

config = {
    "mysql": {
        "user": "root",
        "pass": "secret",
        "tables": {
            "users": "tb_users"
        }
        # etc
    }
}

You’d access the values as follows:

config["mysql"]["tables"]["users"]

If you are willing to sacrifice the potential to compute expressions inside your config tree, you could use YAML and end up with a more readable config file like this:

mysql:
  - user: root
  - pass: secret
  - tables:
    - users: tb_users

and use a library like PyYAML to conventiently parse and access the config file


回答 2

我喜欢用于小型应用程序的解决方案:

class App:
  __conf = {
    "username": "",
    "password": "",
    "MYSQL_PORT": 3306,
    "MYSQL_DATABASE": 'mydb',
    "MYSQL_DATABASE_TABLES": ['tb_users', 'tb_groups']
  }
  __setters = ["username", "password"]

  @staticmethod
  def config(name):
    return App.__conf[name]

  @staticmethod
  def set(name, value):
    if name in App.__setters:
      App.__conf[name] = value
    else:
      raise NameError("Name not accepted in set() method")

然后用法是:

if __name__ == "__main__":
   # from config import App
   App.config("MYSQL_PORT")     # return 3306
   App.set("username", "hi")    # set new username value
   App.config("username")       # return "hi"
   App.set("MYSQL_PORT", "abc") # this raises NameError

..您应该喜欢它,因为:

  • 使用类变量(无需传递对象/无需单例),
  • 使用封装的内置类型,看起来像是在上的方法调用App
  • 可以控制个人配置的不变性可变全局变量是最差的全局变量
  • 在您的源代码中提高常规名称的访问/可读性
  • 是一个简单的类,但是强制进行结构化访问,一种替代方法是使用@property,但是每个项目需要更多的变量处理代码,并且是基于对象的。
  • 只需进行最小的更改即可添加新的配置项并设置其可变性。

-编辑-:对于大型应用程序,将值存储在YAML(即属性)文件中并将其作为不可变数据读取是一种更好的方法(即blubb / ohaal的答案)。对于小型应用程序,上面的解决方案更简单。

I like this solution for small applications:

class App:
  __conf = {
    "username": "",
    "password": "",
    "MYSQL_PORT": 3306,
    "MYSQL_DATABASE": 'mydb',
    "MYSQL_DATABASE_TABLES": ['tb_users', 'tb_groups']
  }
  __setters = ["username", "password"]

  @staticmethod
  def config(name):
    return App.__conf[name]

  @staticmethod
  def set(name, value):
    if name in App.__setters:
      App.__conf[name] = value
    else:
      raise NameError("Name not accepted in set() method")

And then usage is:

if __name__ == "__main__":
   # from config import App
   App.config("MYSQL_PORT")     # return 3306
   App.set("username", "hi")    # set new username value
   App.config("username")       # return "hi"
   App.set("MYSQL_PORT", "abc") # this raises NameError

.. you should like it because:

  • uses class variables (no object to pass around/ no singleton required),
  • uses encapsulated built-in types and looks like (is) a method call on App,
  • has control over individual config immutability, mutable globals are the worst kind of globals.
  • promotes conventional and well named access / readability in your source code
  • is a simple class but enforces structured access, an alternative is to use @property, but that requires more variable handling code per item and is object-based.
  • requires minimal changes to add new config items and set its mutability.

–Edit–: For large applications, storing values in a YAML (i.e. properties) file and reading that in as immutable data is a better approach (i.e. blubb/ohaal’s answer). For small applications, this solution above is simpler.


回答 3

使用类怎么样?

# config.py
class MYSQL:
    PORT = 3306
    DATABASE = 'mydb'
    DATABASE_TABLES = ['tb_users', 'tb_groups']

# main.py
from config import MYSQL

print(MYSQL.PORT) # 3306

How about using classes?

# config.py
class MYSQL:
    PORT = 3306
    DATABASE = 'mydb'
    DATABASE_TABLES = ['tb_users', 'tb_groups']

# main.py
from config import MYSQL

print(MYSQL.PORT) # 3306

回答 4

类似于blubb的答案。我建议使用lambda函数构建它们以减少代码。像这样:

User = lambda passwd, hair, name: {'password':passwd, 'hair':hair, 'name':name}

#Col      Username       Password      Hair Color  Real Name
config = {'st3v3' : User('password',   'blonde',   'Steve Booker'),
          'blubb' : User('12345678',   'black',    'Bubb Ohaal'),
          'suprM' : User('kryptonite', 'black',    'Clark Kent'),
          #...
         }
#...

config['st3v3']['password']  #> password
config['blubb']['hair']      #> black

不过,这确实闻起来像您可能想上一堂课。

或者,如MarkM所述,您可以使用 namedtuple

from collections import namedtuple
#...

User = namedtuple('User', ['password', 'hair', 'name']}

#Col      Username       Password      Hair Color  Real Name
config = {'st3v3' : User('password',   'blonde',   'Steve Booker'),
          'blubb' : User('12345678',   'black',    'Bubb Ohaal'),
          'suprM' : User('kryptonite', 'black',    'Clark Kent'),
          #...
         }
#...

config['st3v3'].password   #> passwd
config['blubb'].hair       #> black

Similar to blubb’s answer. I suggest building them with lambda functions to reduce code. Like this:

User = lambda passwd, hair, name: {'password':passwd, 'hair':hair, 'name':name}

#Col      Username       Password      Hair Color  Real Name
config = {'st3v3' : User('password',   'blonde',   'Steve Booker'),
          'blubb' : User('12345678',   'black',    'Bubb Ohaal'),
          'suprM' : User('kryptonite', 'black',    'Clark Kent'),
          #...
         }
#...

config['st3v3']['password']  #> password
config['blubb']['hair']      #> black

This does smell like you may want to make a class, though.

Or, as MarkM noted, you could use namedtuple

from collections import namedtuple
#...

User = namedtuple('User', ['password', 'hair', 'name']}

#Col      Username       Password      Hair Color  Real Name
config = {'st3v3' : User('password',   'blonde',   'Steve Booker'),
          'blubb' : User('12345678',   'black',    'Bubb Ohaal'),
          'suprM' : User('kryptonite', 'black',    'Clark Kent'),
          #...
         }
#...

config['st3v3'].password   #> passwd
config['blubb'].hair       #> black

回答 5

我使用的赫斯基想法略有不同。创建一个名为“ globals”(或您喜欢的文件)的文件,然后在其中定义多个类,如下所示:

#globals.py

class dbinfo :      # for database globals
    username = 'abcd'
    password = 'xyz'

class runtime :
    debug = False
    output = 'stdio'

然后,如果您有两个代码文件c1.py和c2.py,则两者都可以位于顶部

import globals as gl

现在,所有代码都可以访问和设置值,如下所示:

gl.runtime.debug = False
print(gl.dbinfo.username)

人们会忘记存在类,即使没有实例化属于该类成员的对象也是如此。并且类中没有“自我”的变量。在类的所有实例之间共享,即使没有实例也是如此。一旦任何代码更改了“调试”,所有其他代码都将看到更改。

通过将其导入为gl,您可以拥有多个这样的文件和变量,使您可以跨代码文件,函数等访问和设置值,但不会发生命名空间冲突的危险。

这缺少其他方法的一些聪明的错误检查,但是简单易行。

A small variation on Husky’s idea that I use. Make a file called ‘globals’ (or whatever you like) and then define multiple classes in it, as such:

#globals.py

class dbinfo :      # for database globals
    username = 'abcd'
    password = 'xyz'

class runtime :
    debug = False
    output = 'stdio'

Then, if you have two code files c1.py and c2.py, both can have at the top

import globals as gl

Now all code can access and set values, as such:

gl.runtime.debug = False
print(gl.dbinfo.username)

People forget classes exist, even if no object is ever instantiated that is a member of that class. And variables in a class that aren’t preceded by ‘self.’ are shared across all instances of the class, even if there are none. Once ‘debug’ is changed by any code, all other code sees the change.

By importing it as gl, you can have multiple such files and variables that lets you access and set values across code files, functions, etc., but with no danger of namespace collision.

This lacks some of the clever error checking of other approaches, but is simple and easy to follow.


回答 6

坦白地说,我们可能应该考虑使用Python Software Foundation维护的库:

https://docs.python.org/3/library/configparser.html

配置示例:(ini格式,但可用JSON)

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes

[bitbucket.org]
User = hg

[topsecret.server.com]
Port = 50022
ForwardX11 = no

代码示例:

>>> import configparser
>>> config = configparser.ConfigParser()
>>> config.read('example.ini')
>>> config['DEFAULT']['Compression']
'yes'
>>> config['DEFAULT'].getboolean('MyCompression', fallback=True) # get_or_else

使其可全局访问:

import configpaser
class App:
 __conf = None

 @staticmethod
 def config():
  if App.__conf is None:  # Read only once, lazy.
   App.__conf = configparser.ConfigParser()
   App.__conf.read('example.ini')
  return App.__conf

if __name__ == '__main__':
 App.config()['DEFAULT']['MYSQL_PORT']
 # or, better:
 App.config().get(section='DEFAULT', option='MYSQL_PORT', fallback=3306)
 ....

缺点:

  • 不受控制的全局可变状态。

Let’s be honest, we should probably consider using a Python Software Foundation maintained library:

https://docs.python.org/3/library/configparser.html

Config example: (ini format, but JSON available)

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes

[bitbucket.org]
User = hg

[topsecret.server.com]
Port = 50022
ForwardX11 = no

Code example:

>>> import configparser
>>> config = configparser.ConfigParser()
>>> config.read('example.ini')
>>> config['DEFAULT']['Compression']
'yes'
>>> config['DEFAULT'].getboolean('MyCompression', fallback=True) # get_or_else

Making it globally-accessible:

import configpaser
class App:
 __conf = None

 @staticmethod
 def config():
  if App.__conf is None:  # Read only once, lazy.
   App.__conf = configparser.ConfigParser()
   App.__conf.read('example.ini')
  return App.__conf

if __name__ == '__main__':
 App.config()['DEFAULT']['MYSQL_PORT']
 # or, better:
 App.config().get(section='DEFAULT', option='MYSQL_PORT', fallback=3306)
 ....

Downsides:

  • Uncontrolled global mutable state.

回答 7

请检出通过traitlet实现的IPython配置系统,以实现您正在手动执行的类型强制。

在此处进行剪切和粘贴,以符合SO准则,而不仅仅是随着链接的内容随时间变化而删除链接。

特征文档

这是我们希望我们的配置系统具有的主要要求:

支持分层配置信息。

与命令行选项解析器完全集成。通常,您想读取配置文件,然后使用命令行选项覆盖某些值。我们的配置系统使该过程自动化,并允许将每个命令行选项链接到将被覆盖的配置层次结构中的特定属性。

配置文件本身就是有效的Python代码。这完成了很多事情。首先,可以将逻辑放入配置文件中,以根据操作系统,网络设置,Python版本等设置属性。其次,Python具有用于访问分层数据结构的超简单语法,即常规属性访问(Foo。 Bar.Bam.name)。第三,使用Python可使用户轻松地将配置属性从一个配置文件导入到另一个。第四,即使Python是动态类型的,它也确实具有可以在运行时检查的类型。因此,配置文件中的1是整数’1’,而’1’是字符串。

一种在运行时将配置信息获取到需要它的类的全自动方法。编写遍历配置层次结构以提取特定属性的代码很痛苦。当您具有包含数百个属性的复杂配置信息时,这会让您想哭。

类型检查和验证不需要在运行时之前静态地指定整个配置层次结构。Python是一种非常动态的语言,您并不总是知道程序启动时需要配置的所有内容。

为此,他们基本上定义了3个对象类以及它们之间的关系:

1)配置-基本上是ChainMap /基本dict,具有一些用于合并的增强功能。

2)可配置-基类可将您要配置的所有内容都子类化。

3)应用程序-实例化以执行特定应用程序功能的对象,或用于单一目的软件的主应用程序。

用他们的话说:

应用:应用

应用程序是执行特定工作的过程。最明显的应用是ipython命令行程序。每个应用程序都读取一个或多个配置文件和一组命令行选项,然后为该应用程序生成一个主配置对象。然后,此配置对象将传递到应用程序创建的可配置对象。这些可配置对象实现了应用程序的实际逻辑,并且知道如何在给定配置对象的情况下进行自我配置。

应用程序始终具有配置为Logger的日志属性。这允许对每个应用程序进行集中式日志记录配置。可配置:可配置

可配置的是常规Python类,它充当应用程序中所有主要类的基类。可配置基类是轻量级的,只能做一件事。

此Configurable是HasTraits的子类,它知道如何进行自我配置。具有元数据config = True的类级别特征变为可以从命令行和配置文件配置的值。

开发人员创建可配置的子类,以实现应用程序中的所有逻辑。这些子类中的每一个都有其自己的配置信息,该信息控制如何创建实例。

please check out the IPython configuration system, implemented via traitlets for the type enforcement you are doing manually.

Cut and pasted here to comply with SO guidelines for not just dropping links as the content of links changes over time.

traitlets documentation

Here are the main requirements we wanted our configuration system to have:

Support for hierarchical configuration information.

Full integration with command line option parsers. Often, you want to read a configuration file, but then override some of the values with command line options. Our configuration system automates this process and allows each command line option to be linked to a particular attribute in the configuration hierarchy that it will override.

Configuration files that are themselves valid Python code. This accomplishes many things. First, it becomes possible to put logic in your configuration files that sets attributes based on your operating system, network setup, Python version, etc. Second, Python has a super simple syntax for accessing hierarchical data structures, namely regular attribute access (Foo.Bar.Bam.name). Third, using Python makes it easy for users to import configuration attributes from one configuration file to another. Fourth, even though Python is dynamically typed, it does have types that can be checked at runtime. Thus, a 1 in a config file is the integer ‘1’, while a ‘1’ is a string.

A fully automated method for getting the configuration information to the classes that need it at runtime. Writing code that walks a configuration hierarchy to extract a particular attribute is painful. When you have complex configuration information with hundreds of attributes, this makes you want to cry.

Type checking and validation that doesn’t require the entire configuration hierarchy to be specified statically before runtime. Python is a very dynamic language and you don’t always know everything that needs to be configured when a program starts.

To acheive this they basically define 3 object classes and their relations to each other:

1) Configuration – basically a ChainMap / basic dict with some enhancements for merging.

2) Configurable – base class to subclass all things you’d wish to configure.

3) Application – object that is instantiated to perform a specific application function, or your main application for single purpose software.

In their words:

Application: Application

An application is a process that does a specific job. The most obvious application is the ipython command line program. Each application reads one or more configuration files and a single set of command line options and then produces a master configuration object for the application. This configuration object is then passed to the configurable objects that the application creates. These configurable objects implement the actual logic of the application and know how to configure themselves given the configuration object.

Applications always have a log attribute that is a configured Logger. This allows centralized logging configuration per-application. Configurable: Configurable

A configurable is a regular Python class that serves as a base class for all main classes in an application. The Configurable base class is lightweight and only does one things.

This Configurable is a subclass of HasTraits that knows how to configure itself. Class level traits with the metadata config=True become values that can be configured from the command line and configuration files.

Developers create Configurable subclasses that implement all of the logic in the application. Each of these subclasses has its own configuration information that controls how instances are created.


如何在当前模块上调用setattr()?

问题:如何在当前模块上调用setattr()?

如何将第一个参数“ object”传递给函数setattr(object, name, value),以在当前模块上设置变量?

例如:

setattr(object, "SOME_CONSTANT", 42);

具有与以下相同的效果:

SOME_CONSTANT = 42

在包含这些行的模块中(带有正确的object)。

我在模块级别动态生成几个值,由于无法__getattr__在模块级别定义,所以这是我的后备。

What do I pass as the first parameter “object” to the function setattr(object, name, value), to set variables on the current module?

For example:

setattr(object, "SOME_CONSTANT", 42);

giving the same effect as:

SOME_CONSTANT = 42

within the module containing these lines (with the correct object).

I’m generate several values at the module level dynamically, and as I can’t define __getattr__ at the module level, this is my fallback.


回答 0

import sys

thismodule = sys.modules[__name__]

setattr(thismodule, name, value)

或者,不使用setattr(打破问题的字母,但满足相同的实际目的;-):

globals()[name] = value

注意:在模块范围内,后者等效于:

vars()[name] = value

这更加简洁,但是在函数内部不起作用(vars()给出在其范围内调用的变量:在全局范围内调用时模块的变量,然后可以使用R / W,但在函数中可以使用变量在函数中调用时,然后必须将其视为R / O - Python在线文档对此特定区别可能会造成混淆。

import sys

thismodule = sys.modules[__name__]

setattr(thismodule, name, value)

or, without using setattr (which breaks the letter of the question but satisfies the same practical purposes;-):

globals()[name] = value

Note: at module scope, the latter is equivalent to:

vars()[name] = value

which is a bit more concise, but doesn’t work from within a function (vars() gives the variables of the scope it’s called at: the module’s variables when called at global scope, and then it’s OK to use it R/W, but the function’s variables when called in a function, and then it must be treated as R/O — the Python online docs can be a bit confusing about this specific distinction).


回答 1

如果必须从模块内部设置模块范围的变量,那有什么问题global呢?

# my_module.py

def define_module_scoped_variables():
    global a, b, c
    a, b, c = 'a', ['b'], 3

从而:

>>> import my_module
>>> my_module.define_module_scoped_variables()
>>> a
NameError: name 'a' is not defined
>>> my_module.a
'a'
>>> my_module.b
['b']

If you must set module scoped variables from within the module, what’s wrong with global?

# my_module.py

def define_module_scoped_variables():
    global a, b, c
    a, b, c = 'a', ['b'], 3

thus:

>>> import my_module
>>> my_module.define_module_scoped_variables()
>>> a
NameError: name 'a' is not defined
>>> my_module.a
'a'
>>> my_module.b
['b']

回答 2

在Python 3.7中,您将可以__getattr__在模块级别使用(相关答案)。

根据PEP 562

def __getattr__(name):
    if name == "SOME_CONSTANT":
        return 42
    raise AttributeError(f"module {__name__} has no attribute {name}")

In Python 3.7, you will be able to use __getattr__ at the module level (related answer).

Per PEP 562:

def __getattr__(name):
    if name == "SOME_CONSTANT":
        return 42
    raise AttributeError(f"module {__name__} has no attribute {name}")

回答 3

  1. 你不会的 你会做globals()["SOME_CONSTANT"] = 42
  2. 你不会的 您会将动态生成的内容存储在模块以外的其他位置。
  1. You wouldn’t. You would do globals()["SOME_CONSTANT"] = 42
  2. You wouldn’t. You would store dynamically-generated content somewhere other than a module.

在Python中使用“全局”关键字

问题:在Python中使用“全局”关键字

通过阅读文档,我了解到Python具有一个单独的函数命名空间,如果我想在该函数中使用全局变量,则需要使用global

我正在使用Python 2.7,并且尝试了这个小测试

>>> sub = ['0', '0', '0', '0']
>>> def getJoin():
...     return '.'.join(sub)
...
>>> getJoin()
'0.0.0.0'

即使没有,看起来一切都很好global。我能够毫无问题地访问全局变量。

我有什么想念的吗?另外,以下是Python文档中的内容:

全局语句中列出的名称不得定义为形式参数,也不得在for循环控制目标,类定义,函数定义或import语句中定义。

尽管形式参数和类定义对我来说很有意义,但我无法理解对循环控制目标和函数定义的限制。

What I understand from reading the documentation is that Python has a separate namespace for functions, and if I want to use a global variable in that function, I need to use global.

I’m using Python 2.7 and I tried this little test

>>> sub = ['0', '0', '0', '0']
>>> def getJoin():
...     return '.'.join(sub)
...
>>> getJoin()
'0.0.0.0'

It seems things are working fine even without global. I was able to access global variable without any problem.

Am I missing anything? Also, following is from Python documentation:

Names listed in a global statement must not be defined as formal parameters or in a for loop control target, class definition, function definition, or import statement.

While formal parameters and class definition make sense to me, I’m not able to understand the restriction on for loop control target and function definition.


回答 0

关键字global仅在更改或创建局部上下文中有用,尽管创建全局变量很少被认为是一个很好的解决方案。

def bob():
    me = "locally defined"    # Defined only in local context
    print(me)

bob()
print(me)     # Asking for a global variable

以上将为您提供:

locally defined
Traceback (most recent call last):
  File "file.py", line 9, in <module>
    print(me)
NameError: name 'me' is not defined

如果使用该global语句,则变量将在函数范围之外“可用”,从而有效地成为全局变量。

def bob():
    global me
    me = "locally defined"   # Defined locally but declared as global
    print(me)

bob()
print(me)     # Asking for a global variable

因此,以上代码将为您提供:

locally defined
locally defined

此外,由于python的特性,您还可以global在局部上下文中声明函数,类或其他对象。尽管我会建议不要这样做,因为如果出现问题或需要调试,它会引起噩梦。

The keyword global is only useful to change or create global variables in a local context, although creating global variables is seldom considered a good solution.

def bob():
    me = "locally defined"    # Defined only in local context
    print(me)

bob()
print(me)     # Asking for a global variable

The above will give you:

locally defined
Traceback (most recent call last):
  File "file.py", line 9, in <module>
    print(me)
NameError: name 'me' is not defined

While if you use the global statement, the variable will become available “outside” the scope of the function, effectively becoming a global variable.

def bob():
    global me
    me = "locally defined"   # Defined locally but declared as global
    print(me)

bob()
print(me)     # Asking for a global variable

So the above code will give you:

locally defined
locally defined

In addition, due to the nature of python, you could also use global to declare functions, classes or other objects in a local context. Although I would advise against it since it causes nightmares if something goes wrong or needs debugging.


回答 1

虽然可以在不使用global关键字的情况下访问全局变量,但是如果要修改它们,则必须使用global关键字。例如:

foo = 1
def test():
    foo = 2 # new local foo

def blub():
    global foo
    foo = 3 # changes the value of the global foo

就您而言,您只是在访问列表sub

While you can access global variables without the global keyword, if you want to modify them you have to use the global keyword. For example:

foo = 1
def test():
    foo = 2 # new local foo

def blub():
    global foo
    foo = 3 # changes the value of the global foo

In your case, you’re just accessing the list sub.


回答 2

这是访问名称并将其绑定到作用域之间的区别。

如果只是查找变量以读取其值,则可以访问全局范围和局部范围。

但是,如果您将变量名分配给不在本地范围内的变量,则会将该名称绑定到该范围内(并且如果该名称也作为全局变量存在,则将其隐藏)。

如果希望能够分配给全局名称,则需要告诉解析器使用全局名称,而不是绑定新的本地名称,这就是’global’关键字的作用。

在一个块中的任何地方绑定都会导致该块中每个地方的名称都被绑定,这可能会导致一些看起来很奇怪的后果(例如,UnboundLocalError突然出现在以前的工作代码中)。

>>> a = 1
>>> def p():
    print(a) # accessing global scope, no binding going on
>>> def q():
    a = 3 # binding a name in local scope - hiding global
    print(a)
>>> def r():
    print(a) # fail - a is bound to local scope, but not assigned yet
    a = 4
>>> p()
1
>>> q()
3
>>> r()
Traceback (most recent call last):
  File "<pyshell#35>", line 1, in <module>
    r()
  File "<pyshell#32>", line 2, in r
    print(a) # fail - a is bound to local scope, but not assigned yet
UnboundLocalError: local variable 'a' referenced before assignment
>>> 

This is the difference between accessing the name and binding it within a scope.

If you’re just looking up a variable to read its value, you’ve got access to global as well as local scope.

However if you assign to a variable who’s name isn’t in local scope, you are binding that name into this scope (and if that name also exists as a global, you’ll hide that).

If you want to be able to assign to the global name, you need to tell the parser to use the global name rather than bind a new local name – which is what the ‘global’ keyword does.

Binding anywhere within a block causes the name everywhere in that block to become bound, which can cause some rather odd looking consequences (e.g. UnboundLocalError suddenly appearing in previously working code).

>>> a = 1
>>> def p():
    print(a) # accessing global scope, no binding going on
>>> def q():
    a = 3 # binding a name in local scope - hiding global
    print(a)
>>> def r():
    print(a) # fail - a is bound to local scope, but not assigned yet
    a = 4
>>> p()
1
>>> q()
3
>>> r()
Traceback (most recent call last):
  File "<pyshell#35>", line 1, in <module>
    r()
  File "<pyshell#32>", line 2, in r
    print(a) # fail - a is bound to local scope, but not assigned yet
UnboundLocalError: local variable 'a' referenced before assignment
>>> 

回答 3

其他答案回答您的问题。关于Python的名称,要知道的另一件重要事情是,就每个作用域而言,它们是局部的还是全局的。

考虑一下,例如:

value = 42

def doit():
    print value
    value = 0

doit()
print value

您可能会猜到该value = 0语句将分配给局部变量,而不会影响在doit()函数外部声明的同一变量的值。您可能会惊讶地发现上面的代码无法运行。print value函数内部的语句产生一个UnboundLocalError.

原因是Python注意到在函数的其他地方,您分配了名称value,并且也value未声明global。这使其成为局部变量。但是,当您尝试打印它时,尚未定义本地名称。在这种情况下 Python 不会像其他一些语言那样将名称作为全局变量来查找。本质上,如果您在函数中的任何位置定义了具有相同名称的局部变量,则无法访问全局变量。

The other answers answer your question. Another important thing to know about names in Python is that they are either local or global on a per-scope basis.

Consider this, for example:

value = 42

def doit():
    print value
    value = 0

doit()
print value

You can probably guess that the value = 0 statement will be assigning to a local variable and not affect the value of the same variable declared outside the doit() function. You may be more surprised to discover that the code above won’t run. The statement print value inside the function produces an UnboundLocalError.

The reason is that Python has noticed that, elsewhere in the function, you assign the name value, and also value is nowhere declared global. That makes it a local variable. But when you try to print it, the local name hasn’t been defined yet. Python in this case does not fall back to looking for the name as a global variable, as some other languages do. Essentially, you cannot access a global variable if you have defined a local variable of the same name anywhere in the function.


回答 4

访问名称和分配名称是不同的。就您而言,您只是在访问一个名称。

如果在函数内分配变量,除非您声明全局变量,否则假定该变量为局部变量。如果没有,则假定它是全局的。

>>> x = 1         # global 
>>> def foo():
        print x       # accessing it, it is global

>>> foo()
1
>>> def foo():   
        x = 2        # local x
        print x 

>>> x            # global x
1
>>> foo()        # prints local x
2

Accessing a name and assigning a name are different. In your case, you are just accessing a name.

If you assign to a variable within a function, that variable is assumed to be local unless you declare it global. In the absence of that, it is assumed to be global.

>>> x = 1         # global 
>>> def foo():
        print x       # accessing it, it is global

>>> foo()
1
>>> def foo():   
        x = 2        # local x
        print x 

>>> x            # global x
1
>>> foo()        # prints local x
2

回答 5

  • 您可以访问不带关键字的全局关键字 global
  • 为了能够修改它们,您需要明确声明该关键字是全局的。否则,将在本地范围内声明关键字。

例:

words = [...] 

def contains (word): 
    global words             # <- not really needed
    return (word in words) 

def add (word): 
    global words             # must specify that we're working with a global keyword
    if word not in words: 
        words += [word]
  • You can access global keywords without keyword global
  • To be able to modify them you need to explicitly state that the keyword is global. Otherwise, the keyword will be declared in local scope.

Example:

words = [...] 

def contains (word): 
    global words             # <- not really needed
    return (word in words) 

def add (word): 
    global words             # must specify that we're working with a global keyword
    if word not in words: 
        words += [word]

回答 6

假定在函数外部声明的任何变量都是全局变量,只有在从函数内部(构造函数除外)声明变量时,才必须指定该变量为全局变量。

Any variable declared outside of a function is assumed to be global, it’s only when declaring them from inside of functions (except constructors) that you must specify that the variable be global.


回答 7

Python常见问题解答对此进行了很好的解释

Python中局部和全局变量的规则是什么?

在Python中,仅在函数内部引用的变量是隐式全局的。如果在函数体内任何位置为变量分配了值,除非明确声明为全局变量,否则将假定该变量为局部变量。

尽管起初有些令人惊讶,但片刻的考虑可以解释这一点。一方面,要求global分配变量可防止意外副作用。另一方面,如果global所有全局引用都需要,那么您将一直使用global。您必须声明作为global对内置函数或对导入模块的组件的每个引用。这种混乱将破坏global声明对确定副作用的有用性。

https://docs.python.org/3/faq/programming.html#what-are-the-rules-for-local-and-global-variables-in-python

This is explained well in the Python FAQ

What are the rules for local and global variables in Python?

In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a value anywhere within the function’s body, it’s assumed to be a local unless explicitly declared as global.

Though a bit surprising at first, a moment’s consideration explains this. On one hand, requiring global for assigned variables provides a bar against unintended side-effects. On the other hand, if global was required for all global references, you’d be using global all the time. You’d have to declare as global every reference to a built-in function or to a component of an imported module. This clutter would defeat the usefulness of the global declaration for identifying side-effects.

https://docs.python.org/3/faq/programming.html#what-are-the-rules-for-local-and-global-variables-in-python


回答 8

这意味着您不应执行以下操作:

x = 1

def myfunc():
  global x

  # formal parameter
  def localfunction(x):
    return x+1

  # import statement
  import os.path as x

  # for loop control target
  for x in range(10):
    print x

  # class definition
  class x(object):
    def __init__(self):
      pass

  #function definition
  def x():
    print "I'm bad"

It means that you should not do the following:

x = 1

def myfunc():
  global x

  # formal parameter
  def localfunction(x):
    return x+1

  # import statement
  import os.path as x

  # for loop control target
  for x in range(10):
    print x

  # class definition
  class x(object):
    def __init__(self):
      pass

  #function definition
  def x():
    print "I'm bad"

回答 9

全局使变量“全局”

def out():
    global x
    x = 1
    print(x)
    return


out()

print (x)

这使“ x”的作用类似于函数外部的普通变量。如果您删除了全局变量,那么它将给出一个错误,因为它无法在函数内部打印变量。

def out():
     # Taking out the global will give you an error since the variable x is no longer 'global' or in other words: accessible for other commands
    x = 1
    print(x)
    return


out()

print (x)

Global makes the variable “Global”

def out():
    global x
    x = 1
    print(x)
    return


out()

print (x)

This makes ‘x’ act like a normal variable outside the function. If you took the global out then it would give an error since it cannot print a variable inside a function.

def out():
     # Taking out the global will give you an error since the variable x is no longer 'global' or in other words: accessible for other commands
    x = 1
    print(x)
    return


out()

print (x)

Python函数全局变量?

问题:Python函数全局变量?

我知道我应该避免由于此类混淆而首先使用全局变量,但是如果我要使用它们,以下是使用它们的有效方法吗?(我正在尝试调用在单独函数中创建的变量的全局副本。)

x = "somevalue"

def func_A ():
   global x
   # Do things to x
   return x

def func_B():
   x = func_A()
   # Do things
   return x

func_A()
func_B()

请问x第二函数使用具有全球复制相同的值xfunc_a使用和修改?定义后调用函数时,顺序重要吗?

I know I should avoid using global variables in the first place due to confusion like this, but if I were to use them, is the following a valid way to go about using them? (I am trying to call the global copy of a variable created in a separate function.)

x = "somevalue"

def func_A ():
   global x
   # Do things to x
   return x

def func_B():
   x = func_A()
   # Do things
   return x

func_A()
func_B()

Does the x that the second function uses have the same value of the global copy of x that func_a uses and modifies? When calling the functions after definition, does order matter?


回答 0

如果只想访问全局变量,则只需使用其名称即可。但是,要更改其值,您需要使用global关键字。

例如

global someVar
someVar = 55

这会将全局变量的值更改为55。否则,它将仅将55分配给局部变量。

函数定义列表的顺序无关紧要(假设它们在某种程度上没有相互引用),被调用的顺序确实如此。

If you want to simply access a global variable you just use its name. However to change its value you need to use the global keyword.

E.g.

global someVar
someVar = 55

This would change the value of the global variable to 55. Otherwise it would just assign 55 to a local variable.

The order of function definition listings doesn’t matter (assuming they don’t refer to each other in some way), the order they are called does.


回答 1

在Python范围内,对未在该范围内声明的变量的任何赋值都会创建一个新的局部变量,除非该变量在函数中较早地声明为使用关键字引用了全局范围的变量global

让我们看一下伪代码的修改版本,看看会发生什么:

# Here, we're creating a variable 'x', in the __main__ scope.
x = 'None!'

def func_A():
  # The below declaration lets the function know that we
  #  mean the global 'x' when we refer to that variable, not
  #  any local one

  global x
  x = 'A'
  return x

def func_B():
  # Here, we are somewhat mislead.  We're actually involving two different
  #  variables named 'x'.  One is local to func_B, the other is global.

  # By calling func_A(), we do two things: we're reassigning the value
  #  of the GLOBAL x as part of func_A, and then taking that same value
  #  since it's returned by func_A, and assigning it to a LOCAL variable
  #  named 'x'.     
  x = func_A() # look at this as: x_local = func_A()

  # Here, we're assigning the value of 'B' to the LOCAL x.
  x = 'B' # look at this as: x_local = 'B'

  return x # look at this as: return x_local

实际上,您可以func_B使用named变量重写所有变量,x_local并且其工作方式相同。

该顺序仅与函数执行更改全局x值的操作的顺序一样重要。因此,在我们的示例中,顺序没有关系,因为func_Bcall func_A。在此示例中,顺序很重要:

def a():
  global foo
  foo = 'A'

def b():
  global foo
  foo = 'B'

b()
a()
print foo
# prints 'A' because a() was the last function to modify 'foo'.

请注意,global仅需要修改全局对象。您仍然可以从函数内部访问它们而无需声明global。因此,我们有:

x = 5

def access_only():
  return x
  # This returns whatever the global value of 'x' is

def modify():
  global x
  x = 'modified'
  return x
  # This function makes the global 'x' equal to 'modified', and then returns that value

def create_locally():
  x = 'local!'
  return x
  # This function creates a new local variable named 'x', and sets it as 'local',
  #  and returns that.  The global 'x' is untouched.

请注意,create_locallyaccess_only– 之间的区别access_only是尽管未调用global,但仍访问全局x ,即使create_locally不使用global任何一个,它也会由于分配值而创建本地副本。

这里的困惑是为什么您不应该使用全局变量。

Within a Python scope, any assignment to a variable not already declared within that scope creates a new local variable unless that variable is declared earlier in the function as referring to a globally scoped variable with the keyword global.

Let’s look at a modified version of your pseudocode to see what happens:

# Here, we're creating a variable 'x', in the __main__ scope.
x = 'None!'

def func_A():
  # The below declaration lets the function know that we
  #  mean the global 'x' when we refer to that variable, not
  #  any local one

  global x
  x = 'A'
  return x

def func_B():
  # Here, we are somewhat mislead.  We're actually involving two different
  #  variables named 'x'.  One is local to func_B, the other is global.

  # By calling func_A(), we do two things: we're reassigning the value
  #  of the GLOBAL x as part of func_A, and then taking that same value
  #  since it's returned by func_A, and assigning it to a LOCAL variable
  #  named 'x'.     
  x = func_A() # look at this as: x_local = func_A()

  # Here, we're assigning the value of 'B' to the LOCAL x.
  x = 'B' # look at this as: x_local = 'B'

  return x # look at this as: return x_local

In fact, you could rewrite all of func_B with the variable named x_local and it would work identically.

The order matters only as far as the order in which your functions do operations that change the value of the global x. Thus in our example, order doesn’t matter, since func_B calls func_A. In this example, order does matter:

def a():
  global foo
  foo = 'A'

def b():
  global foo
  foo = 'B'

b()
a()
print foo
# prints 'A' because a() was the last function to modify 'foo'.

Note that global is only required to modify global objects. You can still access them from within a function without declaring global. Thus, we have:

x = 5

def access_only():
  return x
  # This returns whatever the global value of 'x' is

def modify():
  global x
  x = 'modified'
  return x
  # This function makes the global 'x' equal to 'modified', and then returns that value

def create_locally():
  x = 'local!'
  return x
  # This function creates a new local variable named 'x', and sets it as 'local',
  #  and returns that.  The global 'x' is untouched.

Note the difference between create_locally and access_onlyaccess_only is accessing the global x despite not calling global, and even though create_locally doesn’t use global either, it creates a local copy since it’s assigning a value.

The confusion here is why you shouldn’t use global variables.


回答 2

正如其他人指出的那样,global当您希望该函数能够修改全局变量时,需要在该函数中声明一个变量。如果您只想访问它,则不需要global

为了对此进行更详细的说明,“修改”的含义是:如果要重新绑定全局名称,使其指向另一个对象,则必须global在函数中声明该名称。

许多修改(更改)对象的操作不会重新绑定全局名称以指向其他对象,因此它们在不声明函数名称的情况下都是有效global的。

d = {}
l = []
o = type("object", (object,), {})()

def valid():     # these are all valid without declaring any names global!
   d[0] = 1      # changes what's in d, but d still points to the same object
   d[0] += 1     # ditto
   d.clear()     # ditto! d is now empty but it`s still the same object!
   l.append(0)   # l is still the same list but has an additional member
   o.test = 1    # creating new attribute on o, but o is still the same object

As others have noted, you need to declare a variable global in a function when you want that function to be able to modify the global variable. If you only want to access it, then you don’t need global.

To go into a bit more detail on that, what “modify” means is this: if you want to re-bind the global name so it points to a different object, the name must be declared global in the function.

Many operations that modify (mutate) an object do not re-bind the global name to point to a different object, and so they are all valid without declaring the name global in the function.

d = {}
l = []
o = type("object", (object,), {})()

def valid():     # these are all valid without declaring any names global!
   d[0] = 1      # changes what's in d, but d still points to the same object
   d[0] += 1     # ditto
   d.clear()     # ditto! d is now empty but it`s still the same object!
   l.append(0)   # l is still the same list but has an additional member
   o.test = 1    # creating new attribute on o, but o is still the same object

回答 3

这是一种使用全局变量作为参数默认值的情况,吸引了我。

globVar = None    # initialize value of global variable

def func(param = globVar):   # use globVar as default value for param
    print 'param =', param, 'globVar =', globVar  # display values

def test():
    global globVar
    globVar = 42  # change value of global
    func()

test()
=========
output: param = None, globVar = 42

我曾期望param的值为42。首次解析函数func时,Python 2.7评估了globVar的值。更改globVar的值不会影响分配给param的默认值。如下所示,延迟评估可以按照我的需要进行。

def func(param = eval('globVar')):       # this seems to work
    print 'param =', param, 'globVar =', globVar  # display values

或者,如果您想安全,

def func(param = None)):
    if param == None:
        param = globVar
    print 'param =', param, 'globVar =', globVar  # display values

Here is one case that caught me out, using a global as a default value of a parameter.

globVar = None    # initialize value of global variable

def func(param = globVar):   # use globVar as default value for param
    print 'param =', param, 'globVar =', globVar  # display values

def test():
    global globVar
    globVar = 42  # change value of global
    func()

test()
=========
output: param = None, globVar = 42

I had expected param to have a value of 42. Surprise. Python 2.7 evaluated the value of globVar when it first parsed the function func. Changing the value of globVar did not affect the default value assigned to param. Delaying the evaluation, as in the following, worked as I needed it to.

def func(param = eval('globVar')):       # this seems to work
    print 'param =', param, 'globVar =', globVar  # display values

Or, if you want to be safe,

def func(param = None)):
    if param == None:
        param = globVar
    print 'param =', param, 'globVar =', globVar  # display values

回答 4

您可以直接访问函数内部的全局变量。如果要更改该全局变量的值,请使用“ global variable_name”。请参见以下示例:

var = 1
def global_var_change():
      global var
      var = "value changed"
global_var_change() #call the function for changes
print var

一般来说,这不是一个好的编程习惯。通过破坏命名空间逻辑,代码可能变得难以理解和调试。

You can directly access a global variable inside a function. If you want to change the value of that global variable, use “global variable_name”. See the following example:

var = 1
def global_var_change():
      global var
      var = "value changed"
global_var_change() #call the function for changes
print var

Generally speaking, this is not a good programming practice. By breaking namespace logic, code can become difficult to understand and debug.


回答 5

global当您希望更改分配给全局变量的值时,必须使用声明。

您不需要它来读取全局变量。请注意,在对象上调用方法(即使它更改了该对象内的数据)也不会更改保存该对象的变量的值(缺少反射魔术)。

You must use the global declaration when you wish to alter the value assigned to a global variable.

You do not need it to read from a global variable. Note that calling a method on an object (even if it alters the data within that object) does not alter the value of the variable holding that object (absent reflective magic).


在函数中使用全局变量

问题:在函数中使用全局变量

如何在函数中创建或使用全局变量?

如果在一个函数中创建全局变量,如何在另一个函数中使用该全局变量?我是否需要将全局变量存储在需要对其进行访问的函数的局部变量中?

How can I create or use a global variable in a function?

If I create a global variable in one function, how can I use that global variable in another function? Do I need to store the global variable in a local variable of the function which needs its access?


回答 0

您可以在其他函数中使用全局变量,方法是像global在分配给它的每个函数中一样声明它:

globvar = 0

def set_globvar_to_one():
    global globvar    # Needed to modify global copy of globvar
    globvar = 1

def print_globvar():
    print(globvar)     # No need for global declaration to read value of globvar

set_globvar_to_one()
print_globvar()       # Prints 1

我想这是因为全局变量是如此危险,Python希望通过显式要求使用global关键字来确保您真正知道这就是要使用的内容。

如果要在模块之间共享全局变量,请参见其他答案。

You can use a global variable in other functions by declaring it as global in each function that assigns to it:

globvar = 0

def set_globvar_to_one():
    global globvar    # Needed to modify global copy of globvar
    globvar = 1

def print_globvar():
    print(globvar)     # No need for global declaration to read value of globvar

set_globvar_to_one()
print_globvar()       # Prints 1

I imagine the reason for it is that, since global variables are so dangerous, Python wants to make sure that you really know that’s what you’re playing with by explicitly requiring the global keyword.

See other answers if you want to share a global variable across modules.


回答 1

如果我正确地理解了您的情况,那么您所看到的是Python处理本地(函数)和全局(模块)命名空间的结果。

假设您有一个像这样的模块:

# sample.py
myGlobal = 5

def func1():
    myGlobal = 42

def func2():
    print myGlobal

func1()
func2()

您可能希望它显示为42,但是它显示为5。如前所述,如果在中添加’ global‘声明func1()func2()则将输出42。

def func1():
    global myGlobal
    myGlobal = 42

这里发生的事情是,Python假定在函数内的任何位置分配给的任何名称都是该函数的本地名称,除非另有明确说明。如果仅从名称读取,并且该名称在本地不存在,它将尝试在任何包含的作用域(例如,模块的全局作用域)中查找该名称。

myGlobal因此,当您为name分配42时,Python将创建一个局部变量,该局部变量遮盖同名的全局变量。该局部变量超出范围并在返回时被垃圾回收func1();同时,func2()除了(未修改的)全局名称外,再也看不到其他任何内容。请注意,此命名空间决定是在编译时发生的,而不是在运行时发生的-如果在分配它之前先读取myGlobalinside 的值func1(),则会得到一个UnboundLocalError,因为Python已经确定它必须是局部变量,但它尚未具有任何关联的价值。但是通过使用’ global‘语句,您告诉Python应该在其他地方查找该名称,而不是在本地分配它。

(我相信这种行为主要是通过优化本地命名空间而引起的-如果没有这种行为,Python的VM每次在函数内部分配新名称时都需要至少执行三个名称查找(以确保名称没有t已存在于模块/内置级别),这会大大减慢非常常见的操作的速度。)

If I’m understanding your situation correctly, what you’re seeing is the result of how Python handles local (function) and global (module) namespaces.

Say you’ve got a module like this:

# sample.py
myGlobal = 5

def func1():
    myGlobal = 42

def func2():
    print myGlobal

func1()
func2()

You might expecting this to print 42, but instead it prints 5. As has already been mentioned, if you add a ‘global‘ declaration to func1(), then func2() will print 42.

def func1():
    global myGlobal
    myGlobal = 42

What’s going on here is that Python assumes that any name that is assigned to, anywhere within a function, is local to that function unless explicitly told otherwise. If it is only reading from a name, and the name doesn’t exist locally, it will try to look up the name in any containing scopes (e.g. the module’s global scope).

When you assign 42 to the name myGlobal, therefore, Python creates a local variable that shadows the global variable of the same name. That local goes out of scope and is garbage-collected when func1() returns; meanwhile, func2() can never see anything other than the (unmodified) global name. Note that this namespace decision happens at compile time, not at runtime — if you were to read the value of myGlobal inside func1() before you assign to it, you’d get an UnboundLocalError, because Python has already decided that it must be a local variable but it has not had any value associated with it yet. But by using the ‘global‘ statement, you tell Python that it should look elsewhere for the name instead of assigning to it locally.

(I believe that this behavior originated largely through an optimization of local namespaces — without this behavior, Python’s VM would need to perform at least three name lookups each time a new name is assigned to inside a function (to ensure that the name didn’t already exist at module/builtin level), which would significantly slow down a very common operation.)


回答 2

您可能想探索命名空间的概念。在Python中,该模块全局数据的自然存放位置:

每个模块都有自己的专用符号表,模块中定义的所有功能都将其用作全局符号表。因此,模块的作者可以在模块中使用全局变量,而不必担心与用户的全局变量的意外冲突。另一方面,如果您知道自己在做什么,则可以使用与引用其功能相同的符号来触摸模块的全局变量modname.itemname

此处描述了模块中全局变量的特定用法- 如何在模块之间共享全局变量?,为了完整起见,这里共享内容:

在单个程序内的模块之间共享信息的规范方法是创建一个特殊的配置模块(通常称为configcfg)。只需将配置模块导入应用程序的所有模块中即可;然后该模块就可以作为全局名称使用。因为每个模块只有一个实例,所以对模块对象所做的任何更改都会在所有地方反映出来。例如:

文件:config.py

x = 0   # Default value of the 'x' configuration setting

档案:mod.py

import config
config.x = 1

档案:main.py

import config
import mod
print config.x

You may want to explore the notion of namespaces. In Python, the module is the natural place for global data:

Each module has its own private symbol table, which is used as the global symbol table by all functions defined in the module. Thus, the author of a module can use global variables in the module without worrying about accidental clashes with a user’s global variables. On the other hand, if you know what you are doing you can touch a module’s global variables with the same notation used to refer to its functions, modname.itemname.

A specific use of global-in-a-module is described here – How do I share global variables across modules?, and for completeness the contents are shared here:

The canonical way to share information across modules within a single program is to create a special configuration module (often called config or cfg). Just import the configuration module in all modules of your application; the module then becomes available as a global name. Because there is only one instance of each module, any changes made to the module object get reflected everywhere. For example:

File: config.py

x = 0   # Default value of the 'x' configuration setting

File: mod.py

import config
config.x = 1

File: main.py

import config
import mod
print config.x

回答 3

Python使用一种简单的启发式方法来确定应从本地和全局加载变量的范围。如果变量名称出现在分配的左侧,但未声明为全局变量,则假定它是局部变量。如果它没有出现在作业的左侧,则假定它是全局的。

>>> import dis
>>> def foo():
...     global bar
...     baz = 5
...     print bar
...     print baz
...     print quux
... 
>>> dis.disassemble(foo.func_code)
  3           0 LOAD_CONST               1 (5)
              3 STORE_FAST               0 (baz)

  4           6 LOAD_GLOBAL              0 (bar)
              9 PRINT_ITEM          
             10 PRINT_NEWLINE       

  5          11 LOAD_FAST                0 (baz)
             14 PRINT_ITEM          
             15 PRINT_NEWLINE       

  6          16 LOAD_GLOBAL              1 (quux)
             19 PRINT_ITEM          
             20 PRINT_NEWLINE       
             21 LOAD_CONST               0 (None)
             24 RETURN_VALUE        
>>> 

查看baz如何出现在赋值的左侧foo(),它是唯一的LOAD_FAST变量。

Python uses a simple heuristic to decide which scope it should load a variable from, between local and global. If a variable name appears on the left hand side of an assignment, but is not declared global, it is assumed to be local. If it does not appear on the left hand side of an assignment, it is assumed to be global.

>>> import dis
>>> def foo():
...     global bar
...     baz = 5
...     print bar
...     print baz
...     print quux
... 
>>> dis.disassemble(foo.func_code)
  3           0 LOAD_CONST               1 (5)
              3 STORE_FAST               0 (baz)

  4           6 LOAD_GLOBAL              0 (bar)
              9 PRINT_ITEM          
             10 PRINT_NEWLINE       

  5          11 LOAD_FAST                0 (baz)
             14 PRINT_ITEM          
             15 PRINT_NEWLINE       

  6          16 LOAD_GLOBAL              1 (quux)
             19 PRINT_ITEM          
             20 PRINT_NEWLINE       
             21 LOAD_CONST               0 (None)
             24 RETURN_VALUE        
>>> 

See how baz, which appears on the left side of an assignment in foo(), is the only LOAD_FAST variable.


回答 4

如果要在函数中引用全局变量,则可以使用global关键字声明哪些变量是全局变量。您不必在所有情况下都使用它(因为这里的人不正确地声称)-如果在本地作用域或定义此函数的函数的作用域中找不到表达式中引用的名称,则在全局范围内查找该名称变量。

但是,如果分配给未在函数中声明为全局变量的新变量,则该变量将隐式声明为局部变量,并且可能使任何现有的具有相同名称的全局变量都模糊不清。

同样,全局变量是有用的,这与某些OOP狂热者相反,特别是对于较小的脚本(OOP过于杀伤)更是如此。

If you want to refer to a global variable in a function, you can use the global keyword to declare which variables are global. You don’t have to use it in all cases (as someone here incorrectly claims) – if the name referenced in an expression cannot be found in local scope or scopes in the functions in which this function is defined, it is looked up among global variables.

However, if you assign to a new variable not declared as global in the function, it is implicitly declared as local, and it can overshadow any existing global variable with the same name.

Also, global variables are useful, contrary to some OOP zealots who claim otherwise – especially for smaller scripts, where OOP is overkill.


回答 5

如果在一个函数中创建全局变量,如何在另一个函数中使用该变量?

我们可以使用以下功能创建一个全局变量:

def create_global_variable():
    global global_variable # must declare it to be a global first
    # modifications are thus reflected on the module's global scope
    global_variable = 'Foo' 

编写函数实际上不会运行其代码。所以我们调用create_global_variable函数:

>>> create_global_variable()

使用全局变量而不进行修改

只要不希望更改它所指向的对象,就可以使用它:

例如,

def use_global_variable():
    return global_variable + '!!!'

现在我们可以使用全局变量:

>>> use_global_variable()
'Foo!!!'

从函数内部修改全局变量

要将全局变量指向另一个对象,需要再次使用global关键字:

def change_global_variable():
    global global_variable
    global_variable = 'Bar'

请注意,编写此函数后,实际上对其进行更改的代码仍未运行:

>>> use_global_variable()
'Foo!!!'

因此,在调用函数之后:

>>> change_global_variable()

我们可以看到全局变量已更改。global_variable现在该名称指向'Bar'

>>> use_global_variable()
'Bar!!!'

请注意,Python中的“全局”不是真正的全局-只是模块级别的全局。因此,它仅适用于在全局模块中编写的函数。函数会记住编写它们的模块,因此当将它们导出到其他模块时,它们仍会在创建它们的模块中查找全局变量。

具有相同名称的局部变量

如果创建具有相同名称的局部变量,它将覆盖全局变量:

def use_local_with_same_name_as_global():
    # bad name for a local variable, though.
    global_variable = 'Baz' 
    return global_variable + '!!!'

>>> use_local_with_same_name_as_global()
'Baz!!!'

但是使用名称错误的局部变量不会更改全局变量:

>>> use_global_variable()
'Bar!!!'

请注意,除非您确切知道自己在做什么并且有充分的理由这样做,否则应避免使用与全局变量同名的局部变量。我还没有遇到这样的原因。

我们在课堂上得到相同的行为

后面有评论问:

如果我想在一个类内的函数内创建一个全局变量,并想在另一个类内的另一个函数内使用该变量,该怎么办?

在这里,我演示了我们在方法中的行为与常规函数中的行为相同:

class Foo:
    def foo(self):
        global global_variable
        global_variable = 'Foo'

class Bar:
    def bar(self):
        return global_variable + '!!!'

Foo().foo()

现在:

>>> Bar().bar()
'Foo!!!'

但是我建议不要使用全局变量,而应使用类属性,以避免使模块命名空间混乱。还要注意,我们self在这里不使用参数-这些可以是类方法(如果从常规cls参数中更改class属性,则很方便)或静态方法(no selfcls)。

If I create a global variable in one function, how can I use that variable in another function?

We can create a global with the following function:

def create_global_variable():
    global global_variable # must declare it to be a global first
    # modifications are thus reflected on the module's global scope
    global_variable = 'Foo' 

Writing a function does not actually run its code. So we call the create_global_variable function:

>>> create_global_variable()

Using globals without modification

You can just use it, so long as you don’t expect to change which object it points to:

For example,

def use_global_variable():
    return global_variable + '!!!'

and now we can use the global variable:

>>> use_global_variable()
'Foo!!!'

Modification of the global variable from inside a function

To point the global variable at a different object, you are required to use the global keyword again:

def change_global_variable():
    global global_variable
    global_variable = 'Bar'

Note that after writing this function, the code actually changing it has still not run:

>>> use_global_variable()
'Foo!!!'

So after calling the function:

>>> change_global_variable()

we can see that the global variable has been changed. The global_variable name now points to 'Bar':

>>> use_global_variable()
'Bar!!!'

Note that “global” in Python is not truly global – it’s only global to the module level. So it is only available to functions written in the modules in which it is global. Functions remember the module in which they are written, so when they are exported into other modules, they still look in the module in which they were created to find global variables.

Local variables with the same name

If you create a local variable with the same name, it will overshadow a global variable:

def use_local_with_same_name_as_global():
    # bad name for a local variable, though.
    global_variable = 'Baz' 
    return global_variable + '!!!'

>>> use_local_with_same_name_as_global()
'Baz!!!'

But using that misnamed local variable does not change the global variable:

>>> use_global_variable()
'Bar!!!'

Note that you should avoid using the local variables with the same names as globals unless you know precisely what you are doing and have a very good reason to do so. I have not yet encountered such a reason.

We get the same behavior in classes

A follow on comment asks:

what to do if I want to create a global variable inside a function inside a class and want to use that variable inside another function inside another class?

Here I demonstrate we get the same behavior in methods as we do in regular functions:

class Foo:
    def foo(self):
        global global_variable
        global_variable = 'Foo'

class Bar:
    def bar(self):
        return global_variable + '!!!'

Foo().foo()

And now:

>>> Bar().bar()
'Foo!!!'

But I would suggest instead of using global variables you use class attributes, to avoid cluttering the module namespace. Also note we don’t use self arguments here – these could be class methods (handy if mutating the class attribute from the usual cls argument) or static methods (no self or cls).


回答 6

除了已经存在的答案之外,还要使其更加混乱:

在Python中,仅在函数内部引用的变量是 隐式全局的。如果在函数体内的任何位置为变量分配了新值,则假定该变量为local。如果在函数内部为变量分配了新值,则该变量是隐式局部变量,您需要将其显式声明为“ global”。

尽管起初有些令人惊讶,但片刻的考虑可以解释这一点。一方面,要求全局分配变量可防止意外副作用。另一方面,如果所有全局引用都需要全局,那么您将一直使用全局。您必须将对内置函数或导入模块的组件的每个引用声明为全局引用。这种混乱将破坏全球宣言对确定副作用的有用性。

资料来源:Python中局部和全局变量的规则是什么?

In addition to already existing answers and to make this more confusing:

In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a new value anywhere within the function’s body, it’s assumed to be a local. If a variable is ever assigned a new value inside the function, the variable is implicitly local, and you need to explicitly declare it as ‘global’.

Though a bit surprising at first, a moment’s consideration explains this. On one hand, requiring global for assigned variables provides a bar against unintended side-effects. On the other hand, if global was required for all global references, you’d be using global all the time. You’d have to declare as global every reference to a built-in function or to a component of an imported module. This clutter would defeat the usefulness of the global declaration for identifying side-effects.

Source: What are the rules for local and global variables in Python?.


回答 7

使用并行执行,如果您不了解发生了什么,全局变量可能会导致意外结果。这是在多处理中使用全局变量的示例。我们可以清楚地看到,每个进程都使用其自己的变量副本:

import multiprocessing
import os
import random
import sys
import time

def worker(new_value):
    old_value = get_value()
    set_value(random.randint(1, 99))
    print('pid=[{pid}] '
          'old_value=[{old_value:2}] '
          'new_value=[{new_value:2}] '
          'get_value=[{get_value:2}]'.format(
          pid=str(os.getpid()),
          old_value=old_value,
          new_value=new_value,
          get_value=get_value()))

def get_value():
    global global_variable
    return global_variable

def set_value(new_value):
    global global_variable
    global_variable = new_value

global_variable = -1

print('before set_value(), get_value() = [%s]' % get_value())
set_value(new_value=-2)
print('after  set_value(), get_value() = [%s]' % get_value())

processPool = multiprocessing.Pool(processes=5)
processPool.map(func=worker, iterable=range(15))

输出:

before set_value(), get_value() = [-1]
after  set_value(), get_value() = [-2]
pid=[53970] old_value=[-2] new_value=[ 0] get_value=[23]
pid=[53971] old_value=[-2] new_value=[ 1] get_value=[42]
pid=[53970] old_value=[23] new_value=[ 4] get_value=[50]
pid=[53970] old_value=[50] new_value=[ 6] get_value=[14]
pid=[53971] old_value=[42] new_value=[ 5] get_value=[31]
pid=[53972] old_value=[-2] new_value=[ 2] get_value=[44]
pid=[53973] old_value=[-2] new_value=[ 3] get_value=[94]
pid=[53970] old_value=[14] new_value=[ 7] get_value=[21]
pid=[53971] old_value=[31] new_value=[ 8] get_value=[34]
pid=[53972] old_value=[44] new_value=[ 9] get_value=[59]
pid=[53973] old_value=[94] new_value=[10] get_value=[87]
pid=[53970] old_value=[21] new_value=[11] get_value=[21]
pid=[53971] old_value=[34] new_value=[12] get_value=[82]
pid=[53972] old_value=[59] new_value=[13] get_value=[ 4]
pid=[53973] old_value=[87] new_value=[14] get_value=[70]

With parallel execution, global variables can cause unexpected results if you don’t understand what is happening. Here is an example of using a global variable within multiprocessing. We can clearly see that each process works with its own copy of the variable:

import multiprocessing
import os
import random
import sys
import time

def worker(new_value):
    old_value = get_value()
    set_value(random.randint(1, 99))
    print('pid=[{pid}] '
          'old_value=[{old_value:2}] '
          'new_value=[{new_value:2}] '
          'get_value=[{get_value:2}]'.format(
          pid=str(os.getpid()),
          old_value=old_value,
          new_value=new_value,
          get_value=get_value()))

def get_value():
    global global_variable
    return global_variable

def set_value(new_value):
    global global_variable
    global_variable = new_value

global_variable = -1

print('before set_value(), get_value() = [%s]' % get_value())
set_value(new_value=-2)
print('after  set_value(), get_value() = [%s]' % get_value())

processPool = multiprocessing.Pool(processes=5)
processPool.map(func=worker, iterable=range(15))

Output:

before set_value(), get_value() = [-1]
after  set_value(), get_value() = [-2]
pid=[53970] old_value=[-2] new_value=[ 0] get_value=[23]
pid=[53971] old_value=[-2] new_value=[ 1] get_value=[42]
pid=[53970] old_value=[23] new_value=[ 4] get_value=[50]
pid=[53970] old_value=[50] new_value=[ 6] get_value=[14]
pid=[53971] old_value=[42] new_value=[ 5] get_value=[31]
pid=[53972] old_value=[-2] new_value=[ 2] get_value=[44]
pid=[53973] old_value=[-2] new_value=[ 3] get_value=[94]
pid=[53970] old_value=[14] new_value=[ 7] get_value=[21]
pid=[53971] old_value=[31] new_value=[ 8] get_value=[34]
pid=[53972] old_value=[44] new_value=[ 9] get_value=[59]
pid=[53973] old_value=[94] new_value=[10] get_value=[87]
pid=[53970] old_value=[21] new_value=[11] get_value=[21]
pid=[53971] old_value=[34] new_value=[12] get_value=[82]
pid=[53972] old_value=[59] new_value=[13] get_value=[ 4]
pid=[53973] old_value=[87] new_value=[14] get_value=[70]

回答 8

您所说的是使用这样的方法:

globvar = 5

def f():
    var = globvar
    print(var)

f()  # Prints 5

但是更好的方法是像这样使用全局变量:

globavar = 5
def f():
    global globvar
    print(globvar)
f()   #prints 5

两者给出相同的输出。

What you are saying is to use the method like this:

globvar = 5

def f():
    var = globvar
    print(var)

f()  # Prints 5

But the better way is to use the global variable like this:

globavar = 5
def f():
    global globvar
    print(globvar)
f()   #prints 5

Both give the same output.


回答 9

事实证明,答案总是很简单。

这是一个小示例模块,具有在main定义中显示它的简单方法:

def five(enterAnumber,sumation):
    global helper
    helper  = enterAnumber + sumation

def isTheNumber():
    return helper

这是如何在main定义中显示它:

import TestPy

def main():
    atest  = TestPy
    atest.five(5,8)
    print(atest.isTheNumber())

if __name__ == '__main__':
    main()

这个简单的代码就是这样,它将执行。希望对您有所帮助。

As it turns out the answer is always simple.

Here is a small sample module with a simple way to show it in a main definition:

def five(enterAnumber,sumation):
    global helper
    helper  = enterAnumber + sumation

def isTheNumber():
    return helper

Here is how to show it in a main definition:

import TestPy

def main():
    atest  = TestPy
    atest.five(5,8)
    print(atest.isTheNumber())

if __name__ == '__main__':
    main()

This simple code works just like that, and it will execute. I hope it helps.


回答 10

您需要在要使用的每个函数中引用全局变量。

如下:

var = "test"

def printGlobalText():
    global var #wWe are telling to explicitly use the global version
    var = "global from printGlobalText fun."
    print "var from printGlobalText: " + var

def printLocalText():
    #We are NOT telling to explicitly use the global version, so we are creating a local variable
    var = "local version from printLocalText fun"
    print "var from printLocalText: " + var

printGlobalText()
printLocalText()
"""
Output Result:
var from printGlobalText: global from printGlobalText fun.
var from printLocalText: local version from printLocalText
[Finished in 0.1s]
"""

You need to reference the global variable in every function you want to use.

As follows:

var = "test"

def printGlobalText():
    global var #wWe are telling to explicitly use the global version
    var = "global from printGlobalText fun."
    print "var from printGlobalText: " + var

def printLocalText():
    #We are NOT telling to explicitly use the global version, so we are creating a local variable
    var = "local version from printLocalText fun"
    print "var from printLocalText: " + var

printGlobalText()
printLocalText()
"""
Output Result:
var from printGlobalText: global from printGlobalText fun.
var from printLocalText: local version from printLocalText
[Finished in 0.1s]
"""

回答 11

您实际上并没有将全局变量存储在本地变量中,而只是创建了对原始全局引用所引用的同一对象的本地引用。请记住,Python中的几乎所有内容都是一个引用对象的名称,在常规操作中不会复制任何内容。

如果不必显式指定标识符何时引用预定义的全局变量,则可能必须显式指定何时标识符是新的局部变量(例如,使用类似“ var”命令的东西)在JavaScript中看到)。由于局部变量在任何严重且不平凡的系统中都比全局变量更普遍,因此在大多数情况下,Python的系统更为有意义。

可能有一种尝试进行猜测的语言,如果存在则使用全局变量,如果不存在则创建本地变量。但是,这很容易出错。例如,导入另一个模块可能会无意中通过该名称引入全局变量,从而改变程序的行为。

You’re not actually storing the global in a local variable, just creating a local reference to the same object that your original global reference refers to. Remember that pretty much everything in Python is a name referring to an object, and nothing gets copied in usual operation.

If you didn’t have to explicitly specify when an identifier was to refer to a predefined global, then you’d presumably have to explicitly specify when an identifier is a new local variable instead (for example, with something like the ‘var’ command seen in JavaScript). Since local variables are more common than global variables in any serious and non-trivial system, Python’s system makes more sense in most cases.

You could have a language which attempted to guess, using a global variable if it existed or creating a local variable if it didn’t. However, that would be very error-prone. For example, importing another module could inadvertently introduce a global variable by that name, changing the behaviour of your program.


回答 12

尝试这个:

def x1():
    global x
    x = 6

def x2():
    global x
    x = x+1
    print x

x = 5
x1()
x2()  # output --> 7

Try this:

def x1():
    global x
    x = 6

def x2():
    global x
    x = x+1
    print x

x = 5
x1()
x2()  # output --> 7

回答 13

如果您有一个具有相同名称的局部变量,则可能要使用globals()函数

globals()['your_global_var'] = 42

In case you have a local variable with the same name, you might want to use the globals() function.

globals()['your_global_var'] = 42

回答 14

接下来,作为附加,使用文件包含所有在本地声明的所有全局变量,然后import as

文件initval.py

Stocksin = 300
Prices = []

文件getstocks.py

import initval as iv

def getmystocks(): 
    iv.Stocksin = getstockcount()


def getmycharts():
    for ic in range(iv.Stocksin):

Following on and as an add on, use a file to contain all global variables all declared locally and then import as:

File initval.py:

Stocksin = 300
Prices = []

File getstocks.py:

import initval as iv

def getmystocks(): 
    iv.Stocksin = getstockcount()


def getmycharts():
    for ic in range(iv.Stocksin):

回答 15

写入全局数组的显式元素显然不需要全局声明,尽管对其进行“批发”确实具有该要求:

import numpy as np

hostValue = 3.14159
hostArray = np.array([2., 3.])
hostMatrix = np.array([[1.0, 0.0],[ 0.0, 1.0]])

def func1():
    global hostValue    # mandatory, else local.
    hostValue = 2.0

def func2():
    global hostValue    # mandatory, else UnboundLocalError.
    hostValue += 1.0

def func3():
    global hostArray    # mandatory, else local.
    hostArray = np.array([14., 15.])

def func4():            # no need for globals
    hostArray[0] = 123.4

def func5():            # no need for globals
    hostArray[1] += 1.0

def func6():            # no need for globals
    hostMatrix[1][1] = 12.

def func7():            # no need for globals
    hostMatrix[0][0] += 0.33

func1()
print "After func1(), hostValue = ", hostValue
func2()
print "After func2(), hostValue = ", hostValue
func3()
print "After func3(), hostArray = ", hostArray
func4()
print "After func4(), hostArray = ", hostArray
func5()
print "After func5(), hostArray = ", hostArray
func6()
print "After func6(), hostMatrix = \n", hostMatrix
func7()
print "After func7(), hostMatrix = \n", hostMatrix

Writing to explicit elements of a global array does not apparently need the global declaration, though writing to it “wholesale” does have that requirement:

import numpy as np

hostValue = 3.14159
hostArray = np.array([2., 3.])
hostMatrix = np.array([[1.0, 0.0],[ 0.0, 1.0]])

def func1():
    global hostValue    # mandatory, else local.
    hostValue = 2.0

def func2():
    global hostValue    # mandatory, else UnboundLocalError.
    hostValue += 1.0

def func3():
    global hostArray    # mandatory, else local.
    hostArray = np.array([14., 15.])

def func4():            # no need for globals
    hostArray[0] = 123.4

def func5():            # no need for globals
    hostArray[1] += 1.0

def func6():            # no need for globals
    hostMatrix[1][1] = 12.

def func7():            # no need for globals
    hostMatrix[0][0] += 0.33

func1()
print "After func1(), hostValue = ", hostValue
func2()
print "After func2(), hostValue = ", hostValue
func3()
print "After func3(), hostArray = ", hostArray
func4()
print "After func4(), hostArray = ", hostArray
func5()
print "After func5(), hostArray = ", hostArray
func6()
print "After func6(), hostMatrix = \n", hostMatrix
func7()
print "After func7(), hostMatrix = \n", hostMatrix

回答 16

我添加此内容是因为我在其他任何答案中都没有看到它,这对于那些在类似问题上苦苦挣扎的人可能很有用。该globals()函数返回一个可变的全局符号字典,您可以在其中“神奇地”使数据可用于其余代码。例如:

from pickle import load
def loaditem(name):
    with open(r"C:\pickle\file\location"+"\{}.dat".format(name), "rb") as openfile:
        globals()[name] = load(openfile)
    return True

from pickle import dump
def dumpfile(name):
    with open(name+".dat", "wb") as outfile:
        dump(globals()[name], outfile)
    return True

只会让您将变量转储/加载到全局命名空间中。超级方便,没有混乱,没有大惊小怪。可以肯定,它仅适用于Python 3。

I’m adding this as I haven’t seen it in any of the other answers and it might be useful for someone struggling with something similar. The globals() function returns a mutable global symbol dictionary where you can “magically” make data available for the rest of your code. For example:

from pickle import load
def loaditem(name):
    with open(r"C:\pickle\file\location"+"\{}.dat".format(name), "rb") as openfile:
        globals()[name] = load(openfile)
    return True

and

from pickle import dump
def dumpfile(name):
    with open(name+".dat", "wb") as outfile:
        dump(globals()[name], outfile)
    return True

Will just let you dump/load variables out of and into the global namespace. Super convenient, no muss, no fuss. Pretty sure it’s Python 3 only.


回答 17

引用要在其中显示更改的类命名空间。

在此示例中,Runner正在使用文件配置中的max。我希望我的测试在跑步者使用它时更改max的值。

main / config.py

max = 15000

main / runner.py

from main import config
def check_threads():
    return max < thread_count 

测试/runner_test.py

from main import runner                # <----- 1. add file
from main.runner import check_threads
class RunnerTest(unittest):
   def test_threads(self):
       runner.max = 0                  # <----- 2. set global 
       check_threads()

Reference the class namespace where you want the change to show up.

In this example, runner is using max from the file config. I want my test to change the value of max when runner is using it.

main/config.py

max = 15000

main/runner.py

from main import config
def check_threads():
    return max < thread_count 

tests/runner_test.py

from main import runner                # <----- 1. add file
from main.runner import check_threads
class RunnerTest(unittest):
   def test_threads(self):
       runner.max = 0                  # <----- 2. set global 
       check_threads()

回答 18

全局变量很好-多重处理除外

与不同平台/环境的多处理相关的全局问题(一方面是Windows / Mac OS,另一方面是Linux)很麻烦。

我将通过一个简单的示例向您展示这个问题,该问题指出了我前一段时间遇到的问题。

如果您想了解Windows / MacO和Linux上为何有所不同的原因,那么您需要知道这是在…上启动新进程的默认机制。

  • Windows / MacO是“生成”的
  • Linux是“ fork”

它们在内存分配和初始化方面有所不同…(但在此不做介绍)。

让我们看一下问题/示例…

import multiprocessing

counter = 0

def do(task_id):
    global counter
    counter +=1
    print(f'task {task_id}: counter = {counter}')

if __name__ == '__main__':

    pool = multiprocessing.Pool(processes=4)
    task_ids = list(range(4))
    pool.map(do, task_ids)

视窗

如果您在Windows上运行此程序(我也想在MacOS上运行),则会得到以下输出…

task 0: counter = 1
task 1: counter = 2
task 2: counter = 3
task 3: counter = 4

的Linux

如果在Linux上运行它,则会得到以下内容。

task 0: counter = 1
task 1: counter = 1
task 2: counter = 1
task 3: counter = 1

Globals are fine – Except with Multiprocessing

Globals in connection with multiprocessing on different platforms/envrionments as Windows/Mac OS on the one side and Linux on the other are troublesome.

I will show you this with a simple example pointing out a problem which I run into some time ago.

If you want to understand, why things are different on Windows/MacOs and Linux you need to know that, the default mechanism to start a new process on …

  • Windows/MacOs is ‘spawn’
  • Linux is ‘fork’

They are different in Memory allocation an initialisation … (but I don’t go into this here).

Let’s have a look at the problem/example …

import multiprocessing

counter = 0

def do(task_id):
    global counter
    counter +=1
    print(f'task {task_id}: counter = {counter}')

if __name__ == '__main__':

    pool = multiprocessing.Pool(processes=4)
    task_ids = list(range(4))
    pool.map(do, task_ids)

Windows

If you run this on Windows (And I suppose on MacOS too), you get the following output …

task 0: counter = 1
task 1: counter = 2
task 2: counter = 3
task 3: counter = 4

Linux

If you run this on Linux, you get the following instead.

task 0: counter = 1
task 1: counter = 1
task 2: counter = 1
task 3: counter = 1