Reputation: 3731
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
Upvotes: 0
Views: 1887
Reputation: 67842
There are lots of different scopes a variable can be bound to, which is what you seem to be confused about. Here are a few:
# a.py
a = 1 # (1) is module scope
class A:
a = 2 # (2) is class scope
def __init__(self, a=3): # (3) is function scope
self.a = a # (4) self.a is object scope
def same_as_class(self):
return self.a == A.a # compare object- and class-scope variables
def same_as_module(self):
return self.a == a # compare object- and module-scope variables
Now see how these different variables (I only called them all a
to make the point, please don't do this for real) are named, and how they all have different values:
>>> import a
>>> a.a
1 # module scope (1)
>>> a.A.a
2 # class scope (2)
>>> obj1 = a.A() # note the argument defaults to 3 (3)
>>> obj1.a # and this value is bound to the object-scope variable (4)
3
>>> obj.same_as_class()
False # compare the object and class values (3 != 2)
>>> obj2 = a.A(2) # now create a new object, giving an explicit value for (3)
>>> obj2.same_as_class()
True
Note we can also change any of these values:
>>> obj1.same_as_module()
False
>>> obj1.a = 1
>>> obj1.same_as_module()
True
For reference, your z.py
above should probably look like:
import a
n = a.Names()
d.userNames()
d = n.name
print d
because a.Name
is a class, but you're trying to refer to an object-scope variable. An object is an instance of a class: I've called my instance n
. Now I have an object, I can get at the object-scope variable. This is equivalent to Goranek's answer.
In terms of my previous example, you were trying to access obj1.a
without having an obj1
or anything like it. I'm not really sure how to make this clearer, without turning this into an introductory essay on OO and Python's type system.
Upvotes: 4
Reputation: 896
class Names:
def userNames(self):
self.name = 'Richard'
import a
c = a.Names()
c.userNames()
what_you_want_is = c.name
Btw, this code makes no sense..but this is apparently what you want
class Names:
def userNames(self, name):
self.name = name
import a
c = a.Names()
c.userNames("Stephen or something")
what_you_want_is = c.name
# what_you_want_is is "Stephen or something"
Upvotes: 3
Reputation: 49886
"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.
Upvotes: 4