【问题标题】:Python 3.7 Class Property Returns ObjectPython 3.7 类属性返回对象
【发布时间】:2021-02-24 15:51:31
【问题描述】:

我正在尝试创建一个具有属性的类。每次我访问该属性时,无论我如何尝试,我都会收到<property object at xxx>。我在 Visual Studio Community 2019 中运行版本 3.7.8。以下是我尝试过的所有不同方式:

class Form(object):
    def __init__(self):
        self._text="New Form test"

    #----------------------------------------------------------
    #example 1
    @property
    def Text(self):
        return _text
    
    @Text.getter
    def get_text(self):
        return self._text

    @Text.setter
    def set_text(self,value):
        self._text=value
    #----------------------------------------------------------
    #example 2
    def get_text(self):
        return self._text

    Text = property(get_text)
    #----------------------------------------------------------
    #example 3
    @property
    def Text(self):
        return self._text
    #----------------------------------------------------------
    #example 4
    @property
    def Text(self):
        return "foo.bar"
    #----------------------------------------------------------
    #example 5
    def get_text(self):
        return self._text

    def set_text(self,value):
        self._text=value

    Text=property()
    Text.getter(get_text)
    Text.setter(set_text)
    #----------------------------------------------------------
    #example 6
    def get_text(self):
        return self._text

    def set_text(self,value):
        self._text=value
    
    Text = property(get_text,set_text)
    #----------------------------------------------------------

我为每个示例添加了标签,并添加了 #6 以及我根据给出的一个答案尝试的另一种方法。我也将 Form(object): 更改为 Form: 没有变化。

【问题讨论】:

  • Wrt "不管我如何尝试" - 显示您用于创建类实例并访问和设置这些属性的代码;正如我所做的in my answer below。例如f = Form() ... 你还在执行什么?
  • import Form global _form _form=Form.Form lbl=Entry(designFormTitlebar) lbl.configure(fg="white",font="Arial 10 bold",background="blue2") lbl.insert (0,_form.Text) lbl.place(x=5,y=5)

标签: python class object properties


【解决方案1】:

为什么不像described in the docs那样做呢。刚才@property修饰的方法已经是getter了,不需要再创建一个getter方法。而对于setter,方法名需要和属性名一样:

class Form:
    def __init__(self):
        self._text = "New Form test"

    @property
    def Text(self):
        return self._text
    
    @Text.setter
    def Text(self, value):
        self._text = value


f = Form()
f.Text
# output: 'New Form test'
f.Text = 'other text'
f.Text
# output: 'other text'

顺便说一句,Python3 中的类不需要继承自 object

其他版本:

# works, only a getter
def get_text(self):
    return self._text

Text = property(get_text)
# works, only a getter
@property
def Text(self):
    return self._text
# works, only a getter which returns 'foo.bar'
@property
def Text(self):
    return "foo.bar"

对于上一个版本,当您使用 Text = property(...) 长格式时,getter 和 setter 必须一起声明:

def get_text(self):
    return self._text

def set_text(self, value):
    self._text = value

Text = property(get_text, set_text)

【讨论】:

  • 您能否更具体地说明您对“为什么不按照文档中描述的方式进行操作”的评论?根据文档,编写代码有三种不同的方法,在我的示例中,您会看到我尝试了这些不同的方法等等。我什至尝试将您的代码复制并粘贴到我的班级中,它仍然给我返回对象而不是文本。我还尝试删除对象的继承而不做任何更改。
  • 在您的某些试验中存在细微变化 - 如果没有变化,您的代码将可以工作。事实上,只有第一个和最后一个变体需要修复。其他3个都很好。这是标准的 Python,没有模块,没有导入。因此,如果它不适用于 VSCommunity - 检查它是否适用于从命令行执行的常规 Python 脚本。而example 6 在您当前版本的问题中就是我上面所说的 - 有效。同样,如果您的安装不适合您,您的安装可能会严重损坏
  • 代码在VS外部运行,没有变化。
  • 欢迎来到 StackOverflow。阅读“What should I do when someone answers my question?”,了解VotingAccepting
  • 我让我的 Rasp Pi 运行并在那里运行代码。它仍然显示属性对象而不是文本值。所以它不能是我电脑上的python安装。
猜你喜欢
  • 2014-03-15
  • 1970-01-01
  • 1970-01-01
  • 2020-03-09
  • 1970-01-01
  • 2020-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多