2012-12-23 30 views
0

我想在其他Python模块使用的变量,像这样:使Python中的变截面模块 - 在一个类和函数

a.py

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

z.py

import a 
d = a.Names.name 
print d 

然而,这并不识别变量name,并收到以下错误:

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

感谢

+4

它应该工作,如果你在模块级别(不在函数中)分配它。 –

+0

你确定吗?这应该工作。 – Joe

+0

该代码可以正常工作 - 您确定这是您的实际代码吗? –

回答

4

有很多不同的范围变量可以绑定到,这是你似乎很困惑。这里有几个:

# 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 

现在看看如何将这些不同的变量(我只叫他们都a,使点,请不要为真正做到这一点)命名,以及它们是如何具有不同的价值:

>>> 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 

报告中,我们还可以更改其中的任何值:

>>> obj1.same_as_module() 
False 
>>> obj1.a = 1 
>>> obj1.same_as_module() 
True 

对于参考ENCE,你上面的z.py或许应该是这样的:

import a 
n = a.Names() 
d.userNames() 
d = n.name 
print d 

因为a.Name类,但是你想引用一个对象范围变量。一个对象是一个类的实例:我调用了我的实例n。现在我有一个对象,我可以在对象范围变量。这相当于戈拉内克的回答。

就我之前的例子而言,您尝试访问obj1.a而没有obj1或类似的东西。我不确定如何更清楚地做到这一点,而不必把它变成面向对象和Python类型系统的介绍性文章。

+0

谢谢你的解释...它确实有助于我的理解更多。但在我的问题不是:'d = a.Names.name'与'obj1 = a.A()obj.a'相同?就像现在它给出一个:'TypeError:__init __()只需要3个参数(1给定)'。谢谢 – user94628

+1

这是一个很好的答案。 – Marcin

+0

@ user94628 - 在您发布的代码中,'names'是一个对象范围变量,如上面的(4)。您需要一个对象(即_类名称的_instance_),因为每个对象都有自己的值,并且它们可以不同。您正在尝试引用'Names.names',但这不存在 - 没有上面的(2)的等价物。 – Useless

4

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

因此,你的问题是不是在你的问题所示。

如果你真的想访问类的变量(和可能的是,你没有),那么这样做:

from othermodule import ClassName 

print ClassName.var_i_want 

你可能想为持有一个实例内访问变量:

from othermodule import ClassName, some_func 

classnameinstance = some_func(blah) 
print classnameinstance.var_i_want 

更新现在你已经完全改变了你的问题,这里的答案是你的新问题:

IN THI S码:

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

name不是方法userNames的激活的可变访问外部。这被称为局部变量。你会在代码更改为创建一个实例变量:

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

然后,如果你有一个变量叫一个实例classnameinstance,你可以这样做:

print classnameinstance.name 

如果变量有这只会工作已通过调用userNames已创建实例。

如果有其他方法可以接收类的实例,则不需要导入类本身。

+0

感谢您的回复。我已经对你的回复给出了我的原始答案,但仍然出现错误:'AttributeError:type object'Names'没有'name'属性。我是python的新手,所以我不确定我做错了什么。谢谢 – user94628

+0

@ user94628那是因为你没有按照我的建议去做。你正在做别的事情。我将展示如何通过更改来访问实例上的变量。你已经做出了这个改变,并试图访问该类上的变量。 – Marcin

+0

我知道这可能很简单,但我只是没有得到它。我对'classnameinstance'如何产生以及如何在另一个模块中使用它感到困惑。 – user94628

3

文件:a.py

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

文件:z.py

import a 
c = a.Names() 
c.userNames() 
what_you_want_is = c.name 

顺便说一句,这个代码是没有sense..but这显然是你想要什么

一个更好.py

class Names: 
    def userNames(self, name): 
     self.name = name 

更好z。py

import a 
c = a.Names() 
c.userNames("Stephen or something") 
what_you_want_is = c.name 
# what_you_want_is is "Stephen or something" 
+1

能给我一个“ - ”的家伙有礼貌地说出原因吗? – Goranek

+2

我不是downvoter,但'c.Names.userNames()'是错误的。 –

+0

哦,你是对的..我的坏 – Goranek

相关问题