帮助定义全局名称

发布于 2024-09-16 07:26:46 字数 466 浏览 8 评论 0原文

我的代码:

def A():
    a = 'A'

    print a

    return

def B():

    print a + ' in B'

    return

当 B() 输入解释器时,我得到

Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "<module1>", line 9, in B
NameError: global name 'a' is not defined

How should I go about Define a?当 B() 输入解释器

编辑时,我希望最终结果是“A in B”: 如果可能的话,我想将 a 的定义保留在 A() 内。

My Code:

def A():
    a = 'A'

    print a

    return

def B():

    print a + ' in B'

    return

When B() is entered into the interpeter I get

Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "<module1>", line 9, in B
NameError: global name 'a' is not defined

How should I go about defining a? I want the end result to be 'A in B', when B() is entered into the interpreter

edit:
I'd like to keep the definition of a within A() if possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

不必你懂 2024-09-23 07:26:46
def A():
    global a
    a = 'A'
    print a

def B():
    global a
    print a + ' in B'

A()
B()

打印:

A
A in B

顺便说一句:你永远不需要在函数末尾有一个简单的“返回”。

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

def B():
    global a
    print a + ' in B'

A()
B()

this prints:

A
A in B

BTW: You never need a plain "return" at the end of a function.

任谁 2024-09-23 07:26:46

我对 Python 还很陌生,您可能想对以下内容持保留态度,但是您是否考虑过将变量 a 以及函数 A() 和 B() 作为类的成员?

class myClass(object):

    def __init__(self):
        self.a = ''

    def A(self):
        self.a = 'A'
        print self.a

    def B(self):
        print self.a + ' in B'


def main():
    stuff = myClass()
    stuff.A()
    stuff.B()

if __name__ == '__main__':
    main()

当我将上面的代码保存在文件中并运行它时,它似乎按预期工作。

i'm pretty new to Python and you might want to take thes following with a grain of salt, but did you consider to have your variable a and the functions A() and B() as members of a class?

class myClass(object):

    def __init__(self):
        self.a = ''

    def A(self):
        self.a = 'A'
        print self.a

    def B(self):
        print self.a + ' in B'


def main():
    stuff = myClass()
    stuff.A()
    stuff.B()

if __name__ == '__main__':
    main()

When i save the code above in a file and run it, it seems to work as expected.

家住魔仙堡 2024-09-23 07:26:46
a = 'A'    
def B():    
    print a + ' in B'
a = 'A'    
def B():    
    print a + ' in B'
空宴 2024-09-23 07:26:46

您可以使用 global 关键字来做到这一点:

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

def B():
    global a
    # ...

但是,使用全局变量通常是一个坏主意 - 您确定没有更好的方法来完成您想做的事情吗?

You can do this by using the global keyword:

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

def B():
    global a
    # ...

However, using global variables is generally a bad idea - are you sure there's not a better way to do what you want to do?

梦一生花开无言 2024-09-23 07:26:46

查看我的回答这个问题。基本上:

创建一个仅包含全局数据的新模块(在您的情况下,假设myGlobals.py):

# create an instance of some data you want to share across modules
a=0

然后您想要访问此数据的每个文件都可以以这种方式执行此操作:

import myGlobals

myGlobals.a = 'something'

所以在您的案件:

import myGlobals

def A():
    myGlobals.a = 'A'
    print myGlobals.a

def B():
    print myGlobals.a + ' in B'

check out my answer from this SO question. Basically:

Create a new module containing only global data (in your case let's say myGlobals.py):

# create an instance of some data you want to share across modules
a=0

and then each file you want to have access to this data can do so in this fashion:

import myGlobals

myGlobals.a = 'something'

so in your case:

import myGlobals

def A():
    myGlobals.a = 'A'
    print myGlobals.a

def B():
    print myGlobals.a + ' in B'
长发绾君心 2024-09-23 07:26:46

只需像这样输入,无需创建函数或类:

global a

a = 'A'

print a 

print a + ' in B'

Just type like this, no need to create fuction or class :

global a

a = 'A'

print a 

print a + ' in B'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文