在Python中访问类的成员变量?

Ada*_*dam 68 python methods return class

class Example(object):
    def the_example(self):
        itsProblem = "problem"

theExample = Example()
print(theExample.itsProblem)
Run Code Online (Sandbox Code Playgroud)

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

def return_itsProblem(self):
    return itsProblem
Run Code Online (Sandbox Code Playgroud)

然而,这也失败了.

e-s*_*tis 118

答案,用几句话说

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

您必须使用self设置和获取实例变量.您可以在__init__方法中进行设置.然后你的代码将是:

class Example(object):
    def __init__(self):
        self.itsProblem = "problem"


theExample = Example()
print(theExample.itsProblem)
Run Code Online (Sandbox Code Playgroud)

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

class Example(object):
    itsProblem = "problem"


theExample = Example()
print(theExample.itsProblem)
print (Example.itsProblem)
Run Code Online (Sandbox Code Playgroud)

但要小心这个,因为它theExample.itsProblem被自动设置为等于Example.itsProblem,但根本不是同一个变量,可以单独更改.

一些解释

在Python中,可以动态创建变量.因此,您可以执行以下操作:

class Example(object):
    pass

Example.itsProblem = "problem"

e = Example()
e.itsSecondProblem = "problem"

print Example.itsProblem == e.itsSecondProblem 
Run Code Online (Sandbox Code Playgroud)

版画

真正

因此,这正是您使用前面的示例所做的.

实际上,在Python中我们使用selfas this,但它不止于此.self是任何对象方法的第一个参数,因为第一个参数始终是对象引用.这是自动的,无论你是否打电话self.

这意味着你可以做到:

class Example(object):
    def __init__(self):
        self.itsProblem = "problem"


theExample = Example()
print(theExample.itsProblem)
Run Code Online (Sandbox Code Playgroud)

要么:

class Example(object):
    def __init__(my_super_self):
        my_super_self.itsProblem = "problem"


theExample = Example()
print(theExample.itsProblem)
Run Code Online (Sandbox Code Playgroud)

它完全一样.ANY对象方法的第一个参数是当前对象,我们只将其self称为约定.而且你只需要为这个对象添加一个变量,就像从外面做的那样.

现在,关于类变量.

当你这样做时:

class Example(object):
    itsProblem = "problem"


theExample = Example()
print(theExample.itsProblem)
Run Code Online (Sandbox Code Playgroud)

你会注意到我们首先设置一个类变量,然后我们访问一个对象(实例)变量.我们从未设置过这个对象变量,但它有效,这怎么可能?

好吧,Python试图先得到对象变量,但是如果它找不到它,就会给你类变量.警告:类变量在实例之间共享,而对象变量则不是.

作为结论,永远不要使用类变量来设置对象变量的默认值.使用__init__了点.

最后,您将了解到Python类是实例,因此也是对象本身,这为理解上述内容提供了新的见解.一旦你意识到这一点,回来后再读一遍.


ken*_*ytm 10

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

class Example(object):
    def the_example(self):
        self.itsProblem = "problem"  # <-- remember the 'self.'

theExample = Example()
theExample.the_example()
print(theExample.itsProblem)
Run Code Online (Sandbox Code Playgroud)

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

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.
Run Code Online (Sandbox Code Playgroud)


and*_*ate 7

如果您有一个实例函数(即一个通过 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 ...
    
Run Code Online (Sandbox Code Playgroud)