Help Defining Global Names
Asked Answered
F

6

1

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.

Fisch answered 29/8, 2010 at 17:1 Comment(0)
J
2

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.

Joses answered 29/8, 2010 at 17:54 Comment(0)
O
3
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.

Ottillia answered 29/8, 2010 at 17:5 Comment(0)
J
2

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.

Joses answered 29/8, 2010 at 17:54 Comment(0)
C
0
a = 'A'    
def B():    
    print a + ' in B'
Canute answered 29/8, 2010 at 17:5 Comment(1)
I'd like to keep the definition of a within A() if possible.Fisch
R
0

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?

Respondent answered 29/8, 2010 at 17:5 Comment(1)
For some reason this isn't working, I get the same name error.Fisch
H
0

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'
Hixson answered 29/8, 2010 at 17:7 Comment(0)
C
-1

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

global a

a = 'A'

print a 

print a + ' in B'
Conk answered 22/5, 2015 at 3:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.