每个实例描述符? [英] Per instance descriptors ?

查看:61
本文介绍了每个实例描述符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在玩一些(可能是奇怪的......)代码,而且我有一个

用于每个实例描述符,即(虚拟代码):


类DummyDescriptor(对象):

def __get __(self,obj,objtype = None):<如果obj是None,则


返回自我

返回getattr(obj,''bar'',''no bar'')br />

class MyClass1(object):

def __init __(self,bar = None):

如果bar不是None:

self.bar = bar

self.baaz = DummyDescriptor()


mc1 = MyClass1(bar =''back'')

mc1.baaz

- > < __ main __。DummyDescriptor对象位于0x2aaaabc6c390>


这当然是人们所期待的......现在我尝试了以下

hack ^ Mworkaround:


类MyClass2(MyClass1):

def __getattribute __(self,key):

v = MyClass1 .__ getattribute __(self,key)

if hasattr(v,''__ get__''):

return v .__ get __(self,self .__ class__)

return v


它似乎*工作得很好:


mc2 = MyClass2(bar =''foo'')

mc2.baaz

- > ''foo''


现在的问题是:

这个方法有没有明显的(或非显而易见的)缺点?


一些上下文:

1 /真实类是

mod_python应用程序中控制器函数的装饰器,并且给出了以前的mod_python经验,

我不想在运行时把这个类本身搞乱......我还是希望

来抽象一些血腥的细节,描述符会很明显

选择这里。


2 /这是生产代码,所以健壮性比

语法糖更重要 - 但是这个语法糖会很好......


任何提示?


TIA

-

bruno desthuilliers

python -c" print''@''。join([''。''。join([w [:: - 1] for w in p .split(''。'')])

p in''o **** @ xiludom.gro''。split(''''')])"

Hi

I''m currently playing with some (possibly weird...) code, and I''d have a
use for per-instance descriptors, ie (dummy code):

class DummyDescriptor(object):
def __get__(self, obj, objtype=None):
if obj is None:
return self
return getattr(obj, ''bar'', ''no bar'')

class MyClass1(object):
def __init__(self, bar=None):
if bar is not None:
self.bar = bar
self.baaz = DummyDescriptor()

mc1 = MyClass1(bar=''back'')
mc1.baaz
-> <__main__.DummyDescriptor object at 0x2aaaabc6c390>

Which is of course what one would expect... Now I tried the following
hack^Mworkaround:

class MyClass2(MyClass1):
def __getattribute__(self, key):
v = MyClass1.__getattribute__(self, key)
if hasattr(v, ''__get__''):
return v.__get__(self, self.__class__)
return v

And it *seems* to work just fine:

mc2 = MyClass2(bar=''foo'')
mc2.baaz
-> ''foo''

Now the question: is there any obvious (or non-obvious) drawback with
this approach ?

A bit of context:
1/ the real class is a decorator for controller functions in a
mod_python application, and given previous experiences with mod_python,
I''d prefer not mess with the class itself at runtime... still I''d like
to abstract some gory details, and descriptors would be an obvious
choice here.

2/ this is for production code, so robustness is more important than
syntactic sugar - but having this syntactic sugar would be nice...

Any hint ?

TIA
--
bruno desthuilliers
python -c "print ''@''.join([''.''.join([w[::-1] for w in p.split(''.'')]) for
p in ''o****@xiludom.gro''.split(''@'')])"

推荐答案



bruno at modulix写道:

bruno at modulix wrote:


我正在玩一些(可能很奇怪) ...)代码,我将为每个实例描述符使用
,即(虚拟代码):


< snip>

现在的问题是:这种方法有没有明显的(或非显而易见的)缺点?


Staticmethods不再工作了:
Hi

I''m currently playing with some (possibly weird...) code, and I''d have a
use for per-instance descriptors, ie (dummy code):
<snip>
Now the question: is there any obvious (or non-obvious) drawback with
this approach ?
Staticmethods won''t work anymore:
class Test (对象):
.... @staticmethod

.... def foo():

....传递

.... def __getattribute __(self,name):

.... v = object .__ getattribute __(self,name)

.... if hasattr (v,''__ get__''):

.... return v .__ get __(self,self .__ class__)

.... return v

.... test = Test()
test.foo()
class Test(object): .... @staticmethod
.... def foo():
.... pass
.... def __getattribute__(self, name):
.... v = object.__getattribute__(self, name)
.... if hasattr(v, ''__get__''):
.... return v.__get__(self, self.__class__)
.... return v
.... test = Test()
test.foo()



Traceback(最近一次调用最后一次) :

文件"< stdin>",第1行,在?

TypeError:foo()不带参数(给定1个)


TIA
-
bruno desthuilliers
python -c" print''@''。join([''。''。join([w [: p:split(''。'')]中的w代表:' - 1)''o o@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ .split(''@'')])"


Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: foo() takes no arguments (1 given)

TIA
--
bruno desthuilliers
python -c "print ''@''.join([''.''.join([w[::-1] for w in p.split(''.'')]) for
p in ''o****@xiludom.gro''.split(''@'')])"




Ziga



Ziga


Ziga Seilnacht写道:
Ziga Seilnacht wrote:
bruno at modulix写道:
bruno at modulix wrote:


我正在玩一些(可能很奇怪.. 。)代码,我将对每个实例描述符使用
,即(虚拟代码):

< snip>
Hi

I''m currently playing with some (possibly weird...) code, and I''d have a
use for per-instance descriptors, ie (dummy code):

<snip>
现在的问题是:
这种方法有任何明显的(或非显而易见的)缺点吗?
Now the question: is there any obvious (or non-obvious) drawback with
this approach ?



Staticmethods不再工作了:
< br>


Staticmethods won''t work anymore:

class Test(object):
... @staticmethod
... def foo():
...传递
... def __getattribute __(self,name):
... v = object .__ getattribute __(self,name)
... if hasattr(v,'' __get__''):
...返回v .__得到__(自我,自我.__班___)
...返回v
...
测试=测试()
test.foo()
class Test(object):
... @staticmethod
... def foo():
... pass
... def __getattribute__(self, name):
... v = object.__getattribute__(self, name)
... if hasattr(v, ''__get__''):
... return v.__get__(self, self.__class__)
... return v
...
test = Test()
test.foo()



回溯(最近一次调用最后一次) :
文件"< stdin>",第1行,在?
TypeError:foo()不带参数(给定1个)



Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: foo() takes no arguments (1 given)




嗯....好吧,我几乎从不使用静态方法,但我需要检查

这个。


(几分钟后)


好​​吧,对课堂方法没有明显的影响。对于静态方法,快速解决方法是:


导入类型

... def __getattribute __(self,name):
... v = object .__ getattribute __(self,name)
...如果不是isinstance(v,types.FunctionType)和hasattr(v,''__ get__''):... return v .__ get __(self,self .__ class__ )
...返回v



Hmmm.... Well, I almost never use staticmethods, but I need to check
this out.

(a few minutes later)

Ok, no apparent impact on classmethods. For staticmethod, a quick fix is:

import types
... def __getattribute__(self, name):
... v = object.__getattribute__(self, name)
... if not isinstance(v, types.FunctionType) and hasattr(v, ''__get__''): ... return v.__get__(self, self.__class__)
... return v



谢谢Ziga。

其他人?任何有理由*不*这样做?或者大概是安全的?

-

bruno desthuilliers

python -c" print''@''。join(['' 。''。加入([w [:: - 1] for p in p.split(''。'')])for

p in''o****@xiludom.gro '.split(''@'')])>


Thanks Ziga.
Anyone else ? Any good reason to *not* do this ? Or is presumably safe ?
--
bruno desthuilliers
python -c "print ''@''.join([''.''.join([w[::-1] for w in p.split(''.'')]) for
p in ''o****@xiludom.gro''.split(''@'')])"


bruno at modulix写道:
bruno at modulix wrote:
Ziga Seilnacht写道:
Ziga Seilnacht wrote:
bruno at modulix写道:
bruno at modulix wrote:


我正在玩一些(可能是奇怪的......)代码,而我每个实例描述符都有一个
用途,即(虚拟代码):
< snip>
Hi

I''m currently playing with some (possibly weird...) code, and I''d have a
use for per-instance descriptors, ie (dummy code):
<snip>
现在的问题是:有没有明显的(或非显而易见的)这种方法的缺点?
Now the question: is there any obvious (or non-obvious) drawback with
this approach ?



....


....

... def __getattribute __(self,name):
... v = object .__ getattribute __(self,name)
...如果不是isinstance(v, types.FunctionType)\
... def __getattribute__(self, name):
... v = object.__getattribute__(self, name)
... if not isinstance(v, types.FunctionType) \


和hasattr(v,''__ get __''):


and hasattr(v, ''__get__''):

... return v .__ get __(self,self .__ class __)
...返回v
... return v.__get__(self, self.__class__)
... return v




我可能会错过你所做的微妙之处,但为什么要压倒

__getattribute__比简单地定义子类中的描述符更理想吗?

ie,

class MyClass3(MyClass1):

def __init__ (self,bar = None):

如果bar不是None:

self.bar = bar

baaz = DummyDescriptor()

Michael


I may be missing the subtlety of what you''re up to, but why is overriding
__getattribute__ more desirable than simply defining the descriptor in a subclass?
i.e.,
class MyClass3(MyClass1):
def __init__(self, bar=None):
if bar is not None:
self.bar = bar
baaz = DummyDescriptor()
Michael


这篇关于每个实例描述符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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