财产和虚拟 [英] property and virtuality

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

问题描述



我的问题是关于属性和方法的虚拟性。我想要创建一个属性,其get和set方法

是虚拟的。我之前在Delphi中遇到过同样的问题,解决方案

是一样的。我创建了一个私有_get方法和一个公共

get方法。前者将称之为后者。同样代表

_set和set。下面是一个例子:


类C1(对象):

def namechanged(self):

"" 在组件名称更改后调用。""

pass

def getname(self):

"""返回组件的名称。


您可以覆盖此方法以提供生成的组件

names。"""

返回self._name

def setname(self,name):

"""设置组件的名称。


您可以覆盖此方法以编程方式控制

组件名称更改。

"""

self._name = name

self.namechanged()

def _getname(self):

""" Internal方法,不要使用"

返回self.getname()

def _setname(self,name):

" ""内部方法,请勿使用""

self.setname(name)

name = property(_getname,_setname," Name")


class C2(C1):

def getname(self):

返回" lala"


C3类(C1):

def _getname(self):

return" lala"

name = property(_getname,None," Name")

c1 = C1()

c2 = C2()

c3 = C3()


c1.name ='' Test1''

c2.name =''Test2''

c3.name =''Test3''

print c1.name#这将打印''Test1''

print c2.name#这将打印''lala''

print c3.name#这将打印''lala''

打印C3.name是C1.name #False


我不能覆盖C2._getname,因为c2.name会打印

''Test2"而不是拉拉。显然,该属性存储了对

get和set方法的引用,并且不可能让它使用新的

方法。创建一个新属性是最糟糕的 - 需要重复代码

和C3.name是C1.name返回False。 :-)这不是一个大问题

因为我找到了解决方案,我想知道是否有更好的方法来支付
" virtualize"物业获取/设定功能。


-

__________________________________________________ _______________

Laszlo Nagy网址: http://designasign.biz

IT顾问邮件: ga ***** @ geochemsource.com


Python永远!


My problem is about properties and the virtuality of the methods. I
would like to create a property whose get and set methods
are virtual. I had the same problems in Delphi before and the solution
was the same. I created a private _get method and a public
get method. The former one will call the latter. The same stands for
_set and set. Here is an example below:

class C1(object):
def namechanged(self):
"""Called after the name of the component has been changed."""
pass
def getname(self):
"""Returns the name of the component.

You can override this method to provide generated component
names."""
return self._name
def setname(self,name):
"""Set the name of the component.

You can override this method to programatically control
component name changes.
"""
self._name = name
self.namechanged()
def _getname(self):
"""Internal method, do not use"""
return self.getname()
def _setname(self,name):
"""Internal method, do not use"""
self.setname(name)
name = property(_getname,_setname,"Name")

class C2(C1):
def getname(self):
return "lala"

class C3(C1):
def _getname(self):
return "lala"
name = property(_getname,None,"Name")
c1 = C1()
c2 = C2()
c3 = C3()

c1.name = ''Test1''
c2.name = ''Test2''
c3.name = ''Test3''
print c1.name # This will print ''Test1''
print c2.name # This will print ''lala''
print c3.name # This will print ''lala''
print C3.name is C1.name # False

I cannot override C2._getname instead, because c2.name would print
''Test2" instead of lala. Clearly, the property stores a reference to the
get and set methods and it is not possible to have it use the new
methods. Creating a new property is the worst - need to duplicate code
and also C3.name is C1.name returns False. :-) It is not a big problem
because I found the solution just I wonder if there is a better way to
"virtualize" property get/set functions.


--
__________________________________________________ _______________
Laszlo Nagy web: http://designasign.biz
IT Consultant mail: ga*****@geochemsource.com

Python forever!

推荐答案

>我不能覆盖C2._getname,因为c2.name会打印
> I cannot override C2._getname instead, because c2.name would print
''Test2"而不是拉拉。显然,该属性存储了对get / set方法的引用,并且不可能让它使用新的
方法。创建一个新属性是最糟糕的 - 需要重复代码
并且C3.name是C1.name返回False。 :-)这不是一个大问题
因为我发现解决方案只是我想知道是否有更好的方法来虚拟化属性获取/设置函数。
''Test2" instead of lala. Clearly, the property stores a reference to the
get and set methods and it is not possible to have it use the new
methods. Creating a new property is the worst - need to duplicate code
and also C3.name is C1.name returns False. :-) It is not a big problem
because I found the solution just I wonder if there is a better way to
"virtualize" property get/set functions.




我不知道有可能像你第一次预期的那样工作。你自己

解释了原因。

但是_maybe_你可以在这里使用lambda - 它创造了间接层




foo = property(lambda self:self.get_foo(),lamda self,v:self.set_foo(v))


开第二个想法,一个元类_might_在这里帮助 - 但它会相当

详细说明:在基类中查找具有getter的属性和

与某些方法同名的setter在当前的类中,并替换它们,或者用它们创建一个新属性(我不确定描述符是否允许改变它们的get / set / del方法) )。我不是百分百确定是否以及如何好b / b好有用(快速破解很容易,但要在悬崖周围运输​​

多重继承需要更加小心导航我担心...)


-

问候,


Diez B. Roggisch


我不知道有可能像你预期的那样有效。你自己
解释了为什么。

但是_maybe_你可以在这里使用lambda - 它创造了一个需要的间接层。

foo = property( lambda self:self.get_foo(),lamda self,v:self.set_foo(v))


太棒了。我会考虑这个并决定哪个更好 - lamba或

私人功能。 Lambda似乎更短了b $ b b b b b b b b b b b b b b b b b b b b b :-)

第二个想法,这里有一个元类_might_帮助 - 但它会更精确:在基类中查找具有getter和
setter的属性命名为当前类中的一些方法,并替换它们,或者用它们创建一个新属性(我不确定描述符是否允许更改它们的get / set / del方法)。我不是百分之百确定是否以及如何好的工作(快速破解会很容易,但要在悬崖周围运输​​多重继承需要更谨慎的导航我担心......)
I''m not aware of possibility that works as you first expected. You yourself
explained why.

But _maybe_ you can use lambda here - that creates the layer of indirection
one needs.

foo = property(lambda self: self.get_foo(), lamda self,v: self.set_foo(v))

Great. I''ll think about this and decide which is better - lamba or
private functions. Lambda seems much
shorter but it is not as clear why it is there. :-)
On second thoughts, a metaclass _might_ help here - but it would be rather
elaborate: look in the baseclasses for properties that have getters and
setters of the same name as some methods in the current class, and replace
them, or create a new property with them (I''m not sure if descriptors
allow changing their get/set/del methods). I''m not 100% sure if and how
good that works (a quick hack would be easy, but to ship around the cliffs
of multiple inheritance requires more careful navigation I fear...)



是的,我感觉一样。使用元类可能是一个很小的帮助,而是很好的,但可能要慢得多。

感谢您的帮助。

-

__________________________________________________ _______________

Laszlo Nagy网站: http:// designasign。商业

IT顾问邮件: ga*****@geochemsource.com


Python永远!


Yes, I feel the same. Using a metaclass could be a small help but rather
elaborate and probably much slower.
Thank you for your help.
--
__________________________________________________ _______________
Laszlo Nagy web: http://designasign.biz
IT Consultant mail: ga*****@geochemsource.com

Python forever!


>大。我会考虑这个并决定哪个更好 - lamba或
> Great. I''ll think about this and decide which is better - lamba or
私有函数。 Lambda似乎更短,但不清楚它为什么会存在。 :-)


我确实在每个属性行上面添加了注释 - 所以有人可能认为'

与编写显式方法的努力相同。或者,你
可以在getters / setter中引入一个命名方案,该命令方案应该被重载,以便它变得清晰 - 至少对于那些知道

项目。但这对整个事情都是如此:)是的,我也有同感。使用元类可能是一个很小的帮助,但相当精致,可能要慢得多。
private functions. Lambda seems much
shorter but it is not as clear why it is there. :-)
I did put comments above each property line - so one might argue that''s
about the same effort as writing the method explicit. Alternatively, you
could introduce a naming scheme in the getters/setters that are supposed to
be overloaded so that it becomes clear - at least for someone knowing the
project. But that''s true for the whole thingy :) Yes, I feel the same. Using a metaclass could be a small help but rather
elaborate and probably much slower.




当然不会慢得多 - 在课堂创建时发生一次 - 并且

结果甚至会更快,因为你可以跳过间接层。


-

问候,


Diez B. Roggisch



Certainly not much slower - that happens once, at class creation time - and
the results would even be faster, as you can skip the layer of indirection.

--
Regards,

Diez B. Roggisch


这篇关于财产和虚拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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