声明属性 [英] Declarative properties

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

问题描述

嗨。


我想在Python中使用声明属性,即。

类似于Common Lisp中defclass中的插槽定义。似乎即使使用库( https://bean-properties.dev.java.net/

)。


我知道''财产''函数在Python中,但它是正常的用法

并不是声明性的,因为我必须编写命令式的getter和

setter:

class Person(object):

def __init __(self,name):

self._name = name

def _get_name (个体经营):

返回self._name

def _set_name(self,new_name):

self._name = new_name

name = property(_get_name,_ set_name)


我想有类似的东西:


class Person(object):

name = property(''_ name'')


我认为这会导致生成实例字段''_name''和

默认的getter和setter。如果我想定制(g |

s)etters,我会手工编写它们,或者更好的是,使用更多声明性的

方法(如@NotNull注释in Java版本。


我可以通过精彩的

内省功能编写解决方案来解决我的问题。但我正在寻找一种标准的和/或

证明这样做的方法。也许有些PEP正在准备这个?


问候,

Artur

Hi.

I would like to have declarative properties in Python, ie. something
like slots definitions in defclass in Common Lisp. It seems that even
Java will have it, using a library ( https://bean-properties.dev.java.net/
).

I know about ''property'' function in Python, but it''s normal usage
isn''t declarative, because I have to code imperatively getters and
setters:

class Person(object):
def __init__(self, name):
self._name = name
def _get_name(self):
return self._name
def _set_name(self, new_name):
self._name = new_name
name = property(_get_name, _set_name)

I would like to have something like that:

class Person(object):
name = property(''_name'')

I assume that this causes "generation" of instance field ''_name'' and
default getters and setters. And if I would like to customize (g|
s)etters, I would write them by hand, or, better, use more declarative
approach (like @NotNull annotations in Java version).

I could probably code a solution to my problem with help of wonderful
introspection functionalities. But I''m looking for a standard and/or
proven way of doing this. Maybe some PEP is being prepared for this?

Regards,
Artur

推荐答案

On Thu,2007年10月11日11:48:18 +0000,Artur Siekielski写道:
On Thu, 11 Oct 2007 11:48:18 +0000, Artur Siekielski wrote:

class Person(object):

def __init __(self,name):

self._name = name

def _get_name(self):

返回自我._name

def _set_name(self,new_name):

self._name = new_name

name = property(_get_name,_ set_name)
class Person(object):
def __init__(self, name):
self._name = name
def _get_name(self):
return self._name
def _set_name(self, new_name):
self._name = new_name
name = property(_get_name, _set_name)



这更容易拼写:


class Person(对象):

def __init __(self,姓名):

self.name = name

This is more easily spelled:

class Person(object):
def __init__(self, name):
self.name = name


我想有类似的东西:


class Person(object):

name = property(''_ name'')


我认为这会导致生成实例字段''_name''和

默认的getter和setter。
I would like to have something like that:

class Person(object):
name = property(''_name'')

I assume that this causes "generation" of instance field ''_name'' and
default getters and setters.



但为什么呢?默认的getter和setter是不必要的,如果你需要

以外的其他东西,你需要更多地写它

明确。


Ciao,

Marc''BlackJack''Rintsch

But why? Default getters and setters are unnecessary and if you need
something other than the default you need to write it anyway more
explicitly.

Ciao,
Marc ''BlackJack'' Rintsch


Artur Siekielskiaécrit:
Artur Siekielski a écrit :

您好。


我想在Python中使用声明属性,即。

类似于Common Lisp中defclass中的插槽定义。似乎即使使用库( https://bean-properties.dev.java.net/

)。


我知道''财产'' function
Hi.

I would like to have declarative properties in Python, ie. something
like slots definitions in defclass in Common Lisp. It seems that even
Java will have it, using a library ( https://bean-properties.dev.java.net/
).

I know about ''property'' function



实际上,它是一个类。

Actually, it''s a class.

Python中的
,但这是正常的用法

不是声明性的,因为我必须编写命令性的getter和

setters:
in Python, but it''s normal usage
isn''t declarative, because I have to code imperatively getters and
setters:



的确。如果

您不需要做某些特定的事情,那么使用属性(AKA计算属性)会是什么?

Indeed. What would be the use of a property (AKA computed attribute) if
you don''t need to do something specific ?


class人(对象):

def __init __(self,name):

self._name = name

def _get_name(self):

返回self._name

def _set_name(self,new_name):

self._name = new_name

name = property (_get_name,_set_name)
class Person(object):
def __init__(self, name):
self._name = name
def _get_name(self):
return self._name
def _set_name(self, new_name):
self._name = new_name
name = property(_get_name, _set_name)



虽然它经常被看作是一个属性的演示代码,但是它实际上完全无用的
。如果您要做的就是获取并设置

属性(我的意思是,无需任何计算),那么只需使用

属性 - 您将始终能够如果需要的话,将它变成一个属性。

While it''s often seen as demonstration code for a property, it''s
actually totally useless. If all you want to do is to get and set an
attribute (I mean, without any computation anywhere), then just use an
attribute - you will always be able to turn it into a property if and
whene the need arises.


我想有类似的东西:


class Person(对象):

name = property(''_ name'')


我认为这会导致& ;生成"实例字段''_name''和

默认的getter和setter。
I would like to have something like that:

class Person(object):
name = property(''_name'')

I assume that this causes "generation" of instance field ''_name'' and
default getters and setters.



到目前为止,这是浪费时间。这正是你用简单的

属性获得的,而且复杂性和开销要小得多。

So far, it''s a waste of time. That''s exactly what you''d get with a plain
attribute, with far less complexity and overhead.


如果我想定制(g |

s)etters,我会手工编写,
And if I would like to customize (g|
s)etters, I would write them by hand,



这让我们回到属性如何实际工作! - )

Which bring us back to how properties actually work !-)


或更好,使用更多声明性

方法(如Java版本中的@NotNull注释)。
or, better, use more declarative
approach (like @NotNull annotations in Java version).


我可以在精彩的

内省功能的帮助下为我的问题编写解决方案。但我正在寻找一种标准的和/或

证明这样做的方法。也许有些PEP正准备这样做?
I could probably code a solution to my problem with help of wonderful
introspection functionalities. But I''m looking for a standard and/or
proven way of doing this. Maybe some PEP is being prepared for this?



我想你想看看描述符协议 - 它是什么

使属性工作,它允许你编写自己的自定义''smart

属性''。如果你想要自动验证/转换,你可以用
编写使用FormEncode包的自定义描述符(我为Elixir启动了

这样的东西,但是没时间完成它至今)。然后看起来像是



class Person(对象):

name = StringField(empty = False)


//等等

HTH

I guess you want to have a look at the descriptor protocol - it''s what
makes properties work, and it allow you to write your own custom ''smart
attributes''. If you want automatic validation/conversion, you could
write custom descriptors working with the FormEncode package (I started
such a thing for Elixir, but had no time to finish it so far). It would
then looks like:

class Person(object):
name = StringField(empty=False)

// etc

HTH


2007年10月11日星期四13:46 :12 +0000,Marc''BlackJack''Rintsch写道:
On Thu, 11 Oct 2007 13:46:12 +0000, Marc ''BlackJack'' Rintsch wrote:

On Thu,2007年10月11日13:04:53 +0000,Artur Siekielski写道:
On Thu, 11 Oct 2007 13:04:53 +0000, Artur Siekielski wrote:

> 10月11日下午2:27,Marc''BlackJack''Rintsch< bj _... @ gmx.netwrote:
>On Oct 11, 2:27 pm, Marc ''BlackJack'' Rintsch <bj_...@gmx.netwrote:

>>但为什么呢?默认的getter和setter是不必要的,如果你需要
以外的东西,你需要更明确地写它。
>>But why? Default getters and setters are unnecessary and if you need
something other than the default you need to write it anyway more
explicitly.


我发现你的方法存在一些问题:

1。如果我使用其他
类直接访问的实例字段''name',
后来我决定实现非标准的getter,我必须重构
''Person''class 字段的名称)。
问题是我无法自动更改''名称''到''_name''
无处不在,因为
在某些地方我想要公共财产价值(例如经过验证和
格式化),而在其他地方我想要原始财产价值。


I see some problems with your approach:

1. If I use instance field ''name'' which is accessed directly by other
classes,
and later I decide to implement nonstandard getter, I must refactor
''Person'' class
and in some places change ''name'' to ''_name'' (assuming this is now the
field''s name).
The problem is that I cannot automatically change ''name'' to ''_name''
everywhere, because
in some places I want public property value (eg. validated and
formatted), and in other
places raw property value.



那又怎样?否则,无论您是否需要,您随身携带*总是*公共财产的行李

和私人属性。至少对我而言,在大多数情况下,
是不必要的。


So what? Otherwise you carry *always* the baggage of a public property
and a private attribute whether you need this or not. At least for me it
would be unnecessary in most cases.



行李携带不需要的方法是你的b $ b计算机为你带来的东西 - IE,99.99%的情况下没什么大不了的。


行李可能修复(AKA概括)如何访问你的属性

是你在你的截止日期即将来临之前徘徊的东西。


这里有一些为你定义这些方法的代码:


#!/ usr / bin / env python

def gimme_set_get(foo,attribute):

lst = [\

''def set_%s(self,value):''%属性,\

''自我._%s =值''%属性,\

''def get_%s(self):''%属性,\

''返回自我._%s''%属性,\

''foo.set_%s = set_%s''%(属性,属性),\

' 'foo.get_%s = get_%s''%(属性,属性)\

]

s =''\ n''。join(lst)

code = compile(s,''< string>'',''exec'')

eval(代码)


class foo:

def __init __(self,value):

self.public_value = v alue

gimme_set_get(foo,''via_accessor_method_only'')


f = foo(1)

f.set_via_accessor_method_only(1 / 9.0)

print f.get_via_accessor_method_only()


print dir(f)

That "baggage" of carrying around "unneeded" methods is something the
computer carries for you - IE, no big deal in 99.99% of all cases.

The "baggage" of possibly fixing (AKA "generalizing") how your attributes
are accessed is something you lug around while your deadline looms.

Here''s some code that defines such methods for you:

#!/usr/bin/env python

def gimme_set_get(foo, attribute):
lst = [ \
''def set_%s(self, value):'' % attribute, \
'' self._%s = value'' % attribute, \
''def get_%s(self):'' % attribute, \
'' return self._%s'' % attribute, \
''foo.set_%s = set_%s'' % (attribute, attribute), \
''foo.get_%s = get_%s'' % (attribute, attribute) \
]
s = ''\n''.join(lst)
code = compile(s, ''<string>'', ''exec'')
eval(code)

class foo:
def __init__(self, value):
self.public_value = value
gimme_set_get(foo, ''via_accessor_method_only'')

f = foo(1)
f.set_via_accessor_method_only(1/9.0)
print f.get_via_accessor_method_only()

print dir(f)


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

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