0

I am a total python beginner and I have a variable created in a class of a file commandline_reader.py that I want to access from another script. I tried to do it by making the variable global, which doesn't work.

myscript.py:

    from commandline_reader import Commandline_Reader
    reader = Commandline_Reader('--get_serial_number')
    reader.run()

    print output

commandline_reader.py:

    class Commandline_Reader:
        def __init__(self,argString=''):
            global output
            output = []

        def run(self):
            # do stuff
            a = 'somevariable'
            output.append(a) 

When I run myscript.py I always get a NameError: name 'output' is not defined. I've read that this is because global variables are only defined within a module. How do I correctly access the output variable in my script?

4 Answers 4

1

ouch. The whole reason object oriented programming takes place is to avoid the use of global variables. Make them instance variables to access them anywhere in the class.

class Commandline_Reader:
    def __init__(self,argString=''):
        self.output = []

    def run(self):
        # do stuff
        a = 'somevariable'
        self.output.append(a) #output is now part of the instance Commandline reader and can be accessed anywhere inside the class. 


 clr = Commandline_Reader(argstring='--get_serial_number')
 clr.run()
 print clr.output
 >>>['somevariable']
Sign up to request clarification or add additional context in comments.

2 Comments

Well there are lots of reasons. But I can't think of a reason why you would ever need global variables in a class.
For instance, you want to share a data between few @staticmethods, you would need a global variable in that class isn't it?
1

Make output an instance attribute:

class Commandline_Reader:
    def __init__(self,argString=''):
        self.output = [] # note use of self here
    def run(self):
        # do stuff
        a = 'somevariable'
        self.output.append(a) # and here

The access it via the instance:

print reader.output

Comments

0

Maybe class attribute is more appropriate for you?

class Commandline_Reader:

    output = []

    def run(self):
        # do stuff
        a = 'somevariable'
        self.output.append(a) 

Comments

-1

Just return the Value from the run() Method

myscript.py:

from commandline_reader import Commandline_Reader
reader = Commandline_Reader('--get_serial_number')
output = reader.run()

print output

commandline_reader.py:

class Commandline_Reader:
    def __init__(self,argString=''):
        self.output = []

    def run(self):
        # do stuff
        a = 'somevariable'
        self.output.append(a)
        return self.output 

4 Comments

You mean self.output, the way it's now, output = [] in init does nothing. Also list.append returns None, so you shouldn't return it.
the scope of output in your code does not know about the init function without an instance attribute self. This will still return a name error.
Thanks, I was hoping there was such a trivial solution!!
@Mailerdaimon you are returning variable, but the thing how to access variables globally accross the modules.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.