属性错误 [英] Property error

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

问题描述

大家好,


i我试图在Python中使用属性,我确信我已经使下面的代码出错了,但是我只是看不出来是什么。


有谁能帮帮我吗?


代码是:


class Person(object):

age = 0

@property

def age():

def fget(个体经营):

返回self.age

def fset(自我,价值):

self。年龄=价值


me =人()

me.age =" 34"

print me.age

我收到错误:


"文件C:\ project,Python \ p2.py,第12行,在< module>

me.age =" 34"

AttributeError:不能设置属性


我究竟做错了什么?


非常感谢提前。

解决方案

什么版本的Python?最新版本不需要


您好,感谢您的帮助!


我使用的是2.5版本,我想我喜欢更多@property装饰器

而不是属性(...)语法。代码是否变化很多使用

@property ??


再次感谢!


< blockquote>

您的示例Dennis,按预期工作。我理解我所犯的错误

。但是当我现在尝试修复原始代码usihn @property时,

给了我同样的错误。

所以,这里是:

类人物(物件):

_age = 0

@property

def age():

def fget(个体经营):

返回self._age

def fset(自我,价值):

self ._age = value


me =人()

me.age = 34

打印me.age

我相信它是非常明显的东西,但我的技能不是(还)

使我能够弄清楚,,,


king kikapu写道:


您的示例Dennis,按预期工作。我理解我所犯的错误

。但是当我现在尝试修复原始代码usihn @property时,

给了我同样的错误。

所以,这里是:

类人物(物件):

_age = 0

@property

def age():

def fget(个体经营):

返回self._age

def fset(自我,价值):

self ._age = value


me =人()

me.age = 34

打印me.age


我相信这是非常明显但我的技能不是(还)

使我能够解决这个问题,,,



@decorator

def f():

#...





def f():

#...

f = decorator(f())


调用age()函数会发生什么?没有明确的

返回语句,因此没有隐式返回,并且


age = property(age())


与age = property(无)相同


i。即你得到一个没有作为吸气剂的财产。你想要的是


>> def star_property(f):



....返回属性(** f())

....
< blockquote class =post_quotes>


>> class Person(object):



.... _age =" unknown"

.... @ star_property

.... def age( ):

.... def fget(self):

....返回self._age

.... def fset (自我,价值):

.... self._age = value

....返回当地人()

... 。


>> person = Person()
person.age



''unknown''


>> person.age = 42
person.age






然而,这是相当hackish,我建议你坚持使用Dennis给出的

标准方法并限制使用属性作为

decorator为只读属性:


>> class Forever42(object) :



.... @property

.... def age(self):return 42

....


>> Forever42 ().age





彼得


Hi to all,

i am trying to use properties in Python and i am sure i have made
something wrong with the below code but i just cannot see what it is.

Can anyone please help me on this ?

The code is :

class Person(object):
age = 0

@property
def age():
def fget(self):
return self.age
def fset(self, value):
self.age = value

me = Person()
me.age = "34"
print me.age
and i am getting the error:

" File "C:\projects\Python\p2.py", line 12, in <module>
me.age = "34"
AttributeError: can''t set attribute "

What exactly i am doing wrong ??

Thanks a lot in advance.

解决方案

What version of Python? Most recent versions don''t need the

Hi, thanks for the help!

I am using 2.5 version and i think i like more the @property decorator
instead of the property(...) syntax. Is the code changing much using
@property ??

Thanks again!



Your example Dennis, work as expected. I understand the mistake i have
made. But when i try to fix the original code usihn @property now, it
gives me the same error.
So, here it is:

class Person(object):
_age = 0

@property
def age():
def fget(self):
return self._age
def fset(self, value):
self._age = value

me = Person()
me.age = 34
print me.age
I am sure it is something very obvious but my skills does not (yet)
enable me to figure this out,,,


king kikapu wrote:

Your example Dennis, work as expected. I understand the mistake i have
made. But when i try to fix the original code usihn @property now, it
gives me the same error.
So, here it is:

class Person(object):
_age = 0

@property
def age():
def fget(self):
return self._age
def fset(self, value):
self._age = value

me = Person()
me.age = 34
print me.age
I am sure it is something very obvious but my skills does not (yet)
enable me to figure this out,,,

@decorator
def f():
# ...

is the same as

def f():
# ...
f = decorator(f())

What happens when your age() function is invoked? There is no explicit
return statement, so None is implicitly returned, and

age = property(age())

is the same as age = property(None)

i. e. you get a property with None as the getter. What you want is

>>def star_property(f):

.... return property(**f())
....

>>class Person(object):

.... _age = "unknown"
.... @star_property
.... def age():
.... def fget(self):
.... return self._age
.... def fset(self, value):
.... self._age = value
.... return locals()
....

>>person = Person()
person.age

''unknown''

>>person.age = 42
person.age

42

However, that is rather hackish, and I recommend that you stick with the
standard approach given by Dennis and limit the use of property as a
decorator to read-only attributes:

>>class Forever42(object):

.... @property
.... def age(self): return 42
....

>>Forever42().age

42

Peter


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

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