python单个整数变量之间的函数 [英] python single integer variable between function

查看:162
本文介绍了python单个整数变量之间的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以在子函数和父函数之间交换不是列表的变量?

  #parent 

list = [0] * 1
singlevalue = 0

#child - 0
def test():
list [0] = list [0] +1
返回列表

#parent
test()#>> 1
test()#>> 2


#child - 1
def test():
singlevalue = singlevalue +1
return singlevalue

#parent
test()>>
#singlevalue = singlevalue +1
#UnboundLocalError:赋值前引用的局部变量'singlevalue'

该示例向您显示带有列表的输出为1并保留下一次执行。
为什么singlevalue有不同的方法?

解决方案

到目前为止,两个答案都提出了一个解决方案,这不是重点的帖子。区别在于关于列表,你正在使用一个引用,而不是像code> singlevalue 那样操作绑定变量。
只要你试着做: l = l + [1] 例如,你会尝试修改绑定的 l 并获得相同的错误。这是因为python的作用范围是如何工作的!



简称

  singlevalue [局部变量] = singlevalue [实际上是一个全局变量,但可以访问本地存储的变量] + 1 [常量] 

单值存储在本地。现在它想要访问那个没有值的本地存储变量,因为它没有被赋值。如果您想在全球范围内储存,您需要使用全球关键字。

 列表[0] [参考列表中的槽] =列表[0] [仍然是同一个槽,它已经有一个值] +1 

因此,没有问题。 :)

更详细



我们可以看看python的字节码,看看加载的区别:

 >>> def m():
... y = 1
... def test():
... y = y + 1
... return y
... test()
>>> m()
UnboundLocalError:赋值前引用的局部变量'y'
>>> dis.dis(m)
[... cur here ... test()]
#< - 这里常量的值是从本地命名空间
3 6 LOAD_CONST 2(<在02A067B8的码对象测试,文件< pyshell#33>,第3行>)
9 MAKE_FUNCTION 0
12 STORE_FAST 1(test)
[ .. cut here ...]

>>> def l():
... li = [1]
... def test():
... li [0] = li [0] + 1
...返回li
... test()
>>> l()
[...此处切...测试内部()]
#< - 这里加载了一个引用!
3 9 LOAD_CLOSURE 0(li)
12 BUILD_TUPLE 1
15 LOAD_CONST 2(< code object test at 02A06698,file< pyshell#28>,line 3>)
18 MAKE_CLOSURE 0
21 STORE_FAST 0(测试)
[...此处剪切...]

由于这篇文章太长,我建议执行上面的命令,看看这里: http://docs.python.org/2/library/dis.html 查看区别!



主要然而,在第一个区块中存在差异:

  2 0 LOAD_CONST 1(1)
3 STORE_FAST 0(y)#< - 将加载常量推到堆栈

2 0 LOAD_CONST 1(1)
3 BUILD_LIST 1
6 STORE_DEREF 0(li)#< - 将值存储在列表中

查看 here

<我希望这有助于消除差异。干杯!

Is there a way to exchange between child and parent functions a variable that is not a list?

#parent

list=[0]*1
singlevalue = 0

#child - 0
def test():
    list[0]= list[0]+1
    return list

#parent
test() #>>1 
test() #>>2


#child - 1
def test():
    singlevalue = singlevalue +1
    return singlevalue

#parent
test() >>
#    singlevalue = singlevalue +1
#UnboundLocalError: local variable 'singlevalue' referenced before assignment

The example show you that with list it output 1 and keep it for next execution. Why the singlevalue have a different way?

解决方案

Both answers so far suggest a solution, which was not the point of the post. The difference is that regarding the list, you are working with a reference and are not manipulating a "bound" variable, as you would do with singlevalue. As soon as you would try to do: l = l + [1] for example, you would try to modify the bound l and get the same error. That is because of how scoping in python work!

In short:

singlevalue [local variable] = singlevalue [actually a global variable but access to locally stored variable] + 1 [constant]

singlevalue is stored locally. Now it wants to access that locally stored variable, which doesn't have a value yet, as it wasn't assigned. IF you want to store it globally you need to use the global keyword.

list[0] [slot in a referenced list] = list[0] [still same slot, which has a value already]+1

Hence, no problem. :)

In more Detail:

We can have a look at python's bytecode here and see the difference in loading:

>>> def m():
...  y = 1
...  def test():
...   y = y + 1
...   return y
...  test()
>>> m()
UnboundLocalError: local variable 'y' referenced before assignment
>>> dis.dis(m)
  [... cur here ... Inside of test()]
              # <-- here the value of the constant is loaded from the local namespace
  3           6 LOAD_CONST               2 (<code object test at 02A067B8, file "<pyshell#33>", line 3>)                   
              9 MAKE_FUNCTION            0
             12 STORE_FAST               1 (test)
  [... cut here ...]

>>> def l():
...  li = [1]
...  def test():
...   li[0] = li[0] + 1
...   return li
...  test()
>>> l()
[... cut here ... Inside of test()]
              # <-- here a reference is loaded!
  3           9 LOAD_CLOSURE             0 (li)
             12 BUILD_TUPLE              1
             15 LOAD_CONST               2 (<code object test at 02A06698, file "<pyshell#28>", line 3>)
             18 MAKE_CLOSURE             0
             21 STORE_FAST               0 (test)
[... cut here ...]

As this post would be getting too long I recommend to execute above's commands and have a look here: http://docs.python.org/2/library/dis.html to see the difference!

The main difference in how things are stored, however, happens in the first block:

  2           0 LOAD_CONST               1 (1)
              3 STORE_FAST               0 (y) # <- push loaded constant to stack

  2           0 LOAD_CONST               1 (1)
              3 BUILD_LIST               1
              6 STORE_DEREF              0 (li) # <- stores the value in the list

Have a look here!

I hope this helps clearing the difference. Cheers!

这篇关于python单个整数变量之间的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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