Python类属性

2024-04-25 21:53:03 发布

您现在位置:Python中文网/ 问答频道 /正文

error print like thiscalling codeclass definition

我不明白为什么__init__被调用了两次,它会产生不同类型的属性?你知道吗

class Test(object):
    def __init__(self, dataobj):
        self._dataobj = dataobj
        print 'called inside test function %s' % type(self._dataobj)

    @property
    def dataobj(self):
        return self._dataobj

Test(locals())给出:

called inside test function <type 'dict'>
called inside test function <class 'Test'>

这似乎是一个pass-ref/value问题,但我不明白为什么它会给出不同的类型 而且叫了两次。非常感谢!你知道吗


Tags: testself类型属性objectinitdeftype
1条回答
网友
1楼 · 发布于 2024-04-25 21:53:03
class Test(object):
    def __init__(self, dataobj):
        self._dataobj = dataobj
        print 'called inside test function %s' % type(self._dataobj),self._dataobj

    @property
    def dataobj(self):
        return self._dataobj

Test(Test(locals()))

我猜你打错了班级考试的电话。否则你就不会得到那个结果。Test(Test(locals())将生成您得到的结果。你知道吗

相关问题 更多 >