属性问题 [英] Problems with properties

查看:92
本文介绍了属性问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,


我一直在学习如何使用python属性。


获取属性访问权限正在运行,但是设置

属性不起作用。


而不是将属性赋值调度到setNothing,

属性对象正在用字符串代替。


我必须在这里做一些非常愚蠢的事情。


有人请指出我的错误,我有凹痕我的额头

这个。


谢谢,

迈克


- -------------------------------------------------- ---------------


来自unittest import TestCase

import unittest

class任务:

def __init __(自我,命令):

self._command =命令


def setNothing(self,value) :

引发AttributeError


def getCommand(self):

返回self._command


命令= prope rty(getCommand,setNothing)

class taskTest(TestCase):


def testTask(self):

t = Task(" ; dir c:")

c = t.command

self.assertEquals(" dir c:",c)


#应该失败,但不是
t.command =" foo Bar"


self.assertEquals(" dir c: ",t.command)


if __name__ ==" __ main __":

unittest.main()

Hello All,

I have been working on learning how to use python properties.

The get property access is working, but the the set
property is not working.

Rather then dispatching the property assignment to setNothing, the
property object is being replaced with a string.

I must be doing something very stupid here.

Could someone please point out my error, I have dents in my forehead
for this one.

Thanks,
Mike

------------------------------------------------------------------

from unittest import TestCase
import unittest
class Task:
def __init__(self,command):
self._command = command

def setNothing(self, value):
raise AttributeError

def getCommand(self):
return self._command

command=property(getCommand, setNothing)
class taskTest(TestCase):

def testTask(self):
t = Task("dir c:")
c = t.command
self.assertEquals("dir c:", c)

# should fail, but doesn''t
t.command = "foo Bar"

self.assertEquals("dir c:", t.command)

if __name__ == "__main__":
unittest.main()

推荐答案

如果你把它改成这个就行了。你应该为一个房产提供一个get和一套

的功能。


class任务:

def __init __(自我,价值):

self._command = value


def setCommand(self,value):

self._command = value

def getCommand(self):

返回self._command


command = property(getCommand,setCommand)


class taskTest(TestCase):

def testTask(self):

t =任务(" dir c:")

c = t.command

self.assertEquals(" dir c:",c)


#应该会失败,但不会

t.command =" foo Bar"

self.assertEquals(" dir c:",t.command)

if __name__ ==" __ main __":

unittest.main()

If you change it to this it works. You should provide a get and a set
function for a property.

class Task:
def __init__(self, value):
self._command = value

def setCommand(self, value):
self._command = value
def getCommand(self):
return self._command

command=property(getCommand, setCommand)

class taskTest(TestCase):
def testTask(self):
t = Task("dir c:")
c = t.command
self.assertEquals("dir c:", c)

# should fail, but doesn''t
t.command = "foo Bar"
self.assertEquals("dir c:", t.command)
if __name__ == "__main__":
unittest.main()


如果你把它改成这个就行了。你应该为一个房产提供一个get和一套

的功能。


class任务:

def __init __(自我,价值):

self._command = value


def setCommand(self,value):

self._command = value

def getCommand(self):

返回self._command


command = property(getCommand,setCommand)


class taskTest(TestCase):

def testTask(self):

t =任务(" dir c:")

c = t.command

self.assertEquals(" dir c:",c)


#应该会失败,但不会

t.command =" foo Bar"

self.assertEquals(" dir c:",t.command)

if __name__ ==" __ main __":

unittest.main()

If you change it to this it works. You should provide a get and a set
function for a property.

class Task:
def __init__(self, value):
self._command = value

def setCommand(self, value):
self._command = value
def getCommand(self):
return self._command

command=property(getCommand, setCommand)

class taskTest(TestCase):
def testTask(self):
t = Task("dir c:")
c = t.command
self.assertEquals("dir c:", c)

# should fail, but doesn''t
t.command = "foo Bar"
self.assertEquals("dir c:", t.command)
if __name__ == "__main__":
unittest.main()


Michael Schneider写道:
Michael Schneider wrote:
获取属性访问权限正在运行,但设置的
属性不起作用。
The get property access is working, but the the set
property is not working.




这些类需要是新风格。使物业正常运作。只需

更改课程任务: toclass Task(object):。


你的setNothing方法是不必要的,如果你没有证明是setter

将自动引发异常。另外(如果你使用的是Python

2.4)你可能想看看装饰器的语法。


所以你的课堂看起来像这样:


class任务(对象):

def __init __(self,command):

self._command = command


@property

def命令(个体经营):

返回self._command

-

Benji York



The classes need to be "new style" for properties to work right. Just
change "class Task:" to "class Task(object):".

Your "setNothing" method is unnecessary, if you don''t proved a "setter"
an exception will be raised automatically. Also (if you''re using Python
2.4) you probably want to look at decorator syntax.

So you''re class could look like this:

class Task(object):
def __init__(self,command):
self._command = command

@property
def command(self):
return self._command
--
Benji York


这篇关于属性问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆