Making a variable cross module in Python - Within a class and function
Question: I'm trying to use a variable in other python modules, like this:

In `a.py`:

    
    
    class Names:
        def userNames(self):
            self.name = 'Richard'
    

In `z.py`:

    
    
    import a
    d = a.Names.name
    print d
    

However this doesn't recognise the variable `name` and the following error is
received:

    
    
    AttributeError: type object 'Names' has no attribute 'name'
    

Thanks


Answer: > "I've checked again and it's because I'm importing from is a Tornado
> Framework and the variable is within a class."

Accordingly, your problem is not the one shown in your question.

If you actually want to access the variable of a class (and likely, you
don't), then do this:

    
    
    from othermodule import ClassName
    
    print ClassName.var_i_want
    

You probably want to access the variable as held inside an instance:

    
    
    from othermodule import ClassName, some_func
    
    classnameinstance = some_func(blah)
    print classnameinstance.var_i_want
    

**Update** Now that you have completely changed your question, here is the
answer to your new question:

IN this code:

    
    
    class Names:
        def userNames(self):
            name = 'Richard'
    

`name` is not a variable accessible outside of the activation of the method
`userNames`. This is known as a local variable. You would create an instance
variable by changing the code to:

    
    
    def userNames(self):
            self.name = 'Richard'
    

Then, if you have an instance in a variable called `classnameinstance` you can
do:

    
    
    print classnameinstance.name
    

This will only work if the variable has been already created on the instance,
as by calling `userNames`.

You don't need to import the class itself if there is some other way to
receive instances of the class.

