天天看点

python 类的成员变量_如何访问Python中的类的成员变量?

class Example(object):

def the_example(self):

itsProblem ="problem"

theExample = Example()

print(theExample.itsProblem)

如何访问类的变量?我试着加上这个定义:

def return_itsProblem(self):

return itsProblem

然而,这也失败了。

标题编辑,关于类"静态"变量的问题:stackoverflow.com/questions/707380/…

简而言之,答案就在这里

在您的示例中,itsProblem是一个局部变量。

必须使用self设置和获取实例变量。您可以在__init__方法中设置它。那么您的代码将是:

class Example(object):

def __init__(self):

self.itsProblem ="problem"

theExample = Example()

print(theExample.itsProblem)

但是如果你想要一个真正的类变量,那么直接使用类名:

class Example(object):

itsProblem ="problem"

theExample = Example()

print(theExample.itsProblem)

print (Example.itsProblem)

但是要注意这一点,因为theExample.itsProblem被自动设置为等于Example.itsProblem,但是根本不是相同的变量,可以独立更改。

一些解释

在Python中,变量可以动态创建。因此,您可以做以下工作:

class Example(object):

pass

Example.itsProblem ="problem"

e = Example()

e.itsSecondProblem ="problem"

print Example.itsProblem == e.itsSecondProblem

打印

True

因此,这正是您在前面的示例中所做的。

实际上,在Python中我们使用self作为this,但是它比这要多一点。self是任何对象方法的第一个参数,因为第一个参数总是对象引用。这是自动的,不管您是否将其称为self。

这意味着你可以:

class Example(object):

def __init__(self):

self.itsProblem ="problem"

theExample = Example()

print(theExample.itsProblem)

或者:

class Example(object):

def __init__(my_super_self):

my_super_self.itsProblem ="problem"

theExample = Example()

print(theExample.itsProblem)

完全一样。任何对象方法的第一个参数都是当前对象,作为约定,我们只调用它self。你给这个对象添加一个变量,和你从外面做的一样。

现在,关于类变量。

当你做的事:

class Example(object):

itsProblem ="problem"

theExample = Example()

print(theExample.itsProblem)

您将注意到,我们首先设置一个类变量,然后访问一个对象(实例)变量。我们从来没有设置过这个对象变量但是它是有效的,这怎么可能呢?

Python试图首先获取对象变量,但如果找不到,它会给你类变量。警告:类变量在实例之间共享,而对象变量不共享。

作为结论,永远不要使用类变量将默认值设置为对象变量。为此使用__init__。

最后,您将了解到Python类是实例,因此是对象本身,这为理解上述内容提供了新的视角。当你意识到这一点的时候,回头再读一遍。

+ 1初始化。这就是它的作用。

你说:"他。它的问题被自动设置为等于实例。它的问题,但不是相同的变量,完全可以独立地改变。"我相信你知道那里发生了什么,所以我建议换一种说法:"它是相同的变量,但它可以为每个对象独立地重新绑定"。

是的,但是绑定是另一种编程语言的概念,比如Java或C(我怀疑OP是),它是完全未知的。然后我会解释什么是绑定,然后是后期绑定,最后是可变对象上引用的问题。时间太长了。我认为有时你必须在理解的祭坛上牺牲精确性。

还有一个@classmethod装饰器(我认为可能是2.7+)值得研究。这里有趣的讨论- stackoverflow.com/questions/12179271/…

名称绑定:我认为给出错误的声明是一个坏主意,无论级别如何。我建议进行这种轻微的编辑:但是要注意这一点,因为theExample.itsProblem被自动设置为等于Example.itsProblem,但是,从实际的角度来看*根本不是相同的变量,可以独立地更改。*:实际上它一开始是相同的对象,但是如果不理解Python的名称绑定,很容易意外地更改它

您声明的是一个局部变量,而不是类变量。要设置实例变量(属性),请使用

class Example(object):

def the_example(self):

self.itsProblem ="problem"  #

theExample = Example()

theExample.the_example()

print(theExample.itsProblem)

要设置类变量(也称为静态成员),请使用

class Example(object):

def the_example(self):

Example.itsProblem ="problem"

# or, type(self).itsProblem ="problem"

# depending what you want to do when the class is derived.

除非类变量在类级别声明一次。你的方法会在每次瞬间重置它,这真的不是你想要的。有关显式self背后的原理,请参见stackoverflow.com/questions/2709821/python-self-explained。

如果您有一个实例函数(即传递self的实例函数),您可以使用self使用self.__class__获得对该类的引用

例如,在tornado下面的代码中创建一个实例来处理get请求,但是我们可以获得get_handler类并使用它来保存riak客户机,因此我们不需要为每个请求创建一个实例。

import tornado.web

import riak

class get_handler(tornado.web.requestHandler):

riak_client = None

def post(self):

cls = self.__class__

if cls.riak_client is None:

cls.riak_client = riak.RiakClient(pb_port=8087, protocol='pbc')

# Additional code to send response to the request ...

实现return语句,如下面的示例所示!你应该表现得很好。我希望它能帮助一些人…

class Example(object):

def the_example(self):

itsProblem ="problem"

return itsProblem

theExample = Example()

print theExample.the_example()

您应该修复代码缩进。这个问题也有更好的答案,你的答案是这些答案的基本变体,而不是替代的解决方案……