Py学习  »  Python

pl/python中的全局变量

user275801 • 4 年前 • 113 次点击  

以下代码在我的python ide中工作正常:

counter = 1000


def increment():
    global counter
    counter += 1


increment()
print(counter)

但是当我复制和粘贴一个pl/python函数中的代码(如下所示)时,它不起作用。

counter = 1000


def increment():
    global counter
    counter += 1


increment()
plpy.notice(counter)

返回的错误消息为:

ERROR:  NameError: name 'counter' is not defined
CONTEXT:  Traceback (most recent call last):
  PL/Python function "testing", line 9, in <module>
    increment()
  PL/Python function "testing", line 6, in increment
    counter += 1
PL/Python function "testing"
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38613
 
113 次点击  
文章 [ 1 ]  |  最新文章 4 年前
user275801
Reply   •   1 楼
user275801    5 年前

好吧,我得把它改成:

varcol = {"counter": 1000}


def increment(varcol):
    varcol["counter"] += 1


increment(varcol)
plpy.notice(varcol["counter"])

相当尴尬,但似乎不允许在pl/python中使用global关键字。如果医生提到这个…