python全局函数global_Python函数全局变量?(Python function global variables?)

Python函数全局变量?(Python function global variables?)

所以我知道我应该首先避免使用全局变量,因为这样的混淆,但是如果我使用它们,以下是使用它们的有效方法? (我试图调用在一个单独的函数中创建的变量的全局副本。)

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与func_a使用和修改的x的全局副本的值是否相同? 定义后调用函数时,顺序是否重要?

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?

原文:https://stackoverflow.com/questions/10588317

更新时间:2019-09-13 09:58

最满意答案

如果你想简单地访问一个全局变量,你只需要使用它的名字。 但是,要更改其值,您需要使用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.

2015-02-28

相关问答

您可以使用相同的global语句列出多个变量。 一个例子: x = 34

y = 32

def f():

global x,y

x = 1

y = 2

这样,函数中使用的全局变量列表可以包含在几行中。 尽管如此,正如@BrenBarn在上面的评论中所述,如果你的函数只是初始化变量,那么就不需要使用函数了。 You can list several variables using the same global statement. An example: x = 34

...

假设SYNC_TOTAL_SIZE没有被声明为局部变量。 它取决于你在全局变量上执行什么操作,只需要读全局变量就不需要声明它是全局变量,但是如果你正在修改全局变量的值,你必须用全局变量来声明它。 Assuming SYNC_TOTAL_SIZE is not declared as local variable. It depends on what operations you are performing on global variables, for just reading the gl

...

如果你想简单地访问一个全局变量,你只需要使用它的名字。 但是,要更改其值,您需要使用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

...

global和赋值都是陈述。 你不能在同一条线上混合它们。 global and assignment are both statements. You can't mix them on the same line.

是的,有两种选择。 首先,您可以传递值而不是使用全局变量。 例如,create_list_and_find_max_and_min可以在本地创建数组并将其返回,然后您可以将它传递给the_smart_way: import random

#----------------------------------------------#

def main():

my_array = create_list_and_find_max_and_min(10)

print th

...

不要使用全局变量。 你几乎从不需要Python,特别是作为初学者。 相反,类Initialization可以有一个变量image_h ,如下所示: class Initialization(object):

def __init__(self):

self.image_h = 0

def parsing(self):

self.image_h = 25

def main():

init = Initialization() //class i

...

为了解决这个问题,我停止尝试在主代码中的多个模块中使用全局变量,并停止导入单个变量。 我基本上最终在moduleB中放置了moduleA(单个函数)。 我在moduleB中为我需要的函数创建了一个类。 主代码和moduleC现在访问这些功能。 我仍然在moduleB中的函数中使用全局变量。 如果我使变量不是全局的,我会得到属性错误。 To solve this issue, I stopped trying to use global variables across multiple modul

...

这基本上是因为Python中的赋值并不意味着你的想法,列表是可变序列 。 列表是Python对象。 名称 board仅仅是该对象的标签或参考 。 因此,当你说new=board这意味着“让名称new引用与名称board相同的对象”。 在Python中,每个对象都有一个唯一的标识符,您可以使用id()来检索它。 让我们创建一个板,完成任务,看看会发生什么: In [1]: board = [[0,0,1],[0,1,0],[1,0,0]]

In [2]: new = board

In [3]:

...

您可以使用inspect来查看堆栈。 我已经定义的这个愚蠢的(命名不佳的函数)似乎可以很好地从调用命名空间中获取全局变量,尽管我还没有对它进行过广泛的测试。 我也不确定它是否适用于不同的python实现。 (我提到这个因为inspect.currentframe函数肯定是依赖于实现的)。 它似乎与Cpython一起使用它的价值。 import inspect

def save(globals=None):

if globals is None:

frames = inspe

...

为了保护程序员免受全局变量的影响,在声明它们使用global variablename之前,不会认为全局变量存在于函数中。 在maincrypt中,您不会声明global newtext也不会通过分配来创建本地新newtext 。 + =运算符要求左手已存在 - 它还没有,因此抛出异常。 顺便说一下,如果你计划在其他任何地方重用这个程序,最好使用类变量而不是全局变量,所以1)如果其他人使用相同的全局变量名称,则一切正常2)你可以有两个加密器实例/ decryptor代码运行和加密/解密不同的东西,

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值