Python 2.6:确定方法是否被继承 [英] Python 2.6: Determining if a method is inherited

查看:104
本文介绍了Python 2.6:确定方法是否被继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我可能很愚蠢(以前发生过这种情况),但我正在努力

修复一些代码破损使用Python 2.6。


我有一些代码在类上查找''__lt__''方法:


if hasattr(clr ,'__ lt__''):


但是 - 在Python 2.6对象中已经增加了

''__ lt__''的默认实现,所以这个测试总是如此返回True。


>> class X(object):传递



....


>> X .__ lt__



< method-wrapper''__lt__''类型对象在0xa15cf0>


>> X .__ lt__ == object .__ lt__



False


那么如何判断X .__ lt__是否从对象继承?我可以在班级的__dict__中查看

- 但是这并不能告诉我是否从基类继承了''__lt__''除了对象。 (查看里面

方法包装repr与正则表达式是不可接受的答案...)


我尝试过的一些事情:
< br>


>> X .__ lt __.__ self__



< class''__ main __。X''>


>> dir(X .__ lt __)



[''__call__'',''__ class__'',''__ cmp__'',''__ delattr__'',''__ doc __'',
''__ format__'','__ getattribute__'',''__ hash__'',''_ _ _ _ _ _ _'''''''_ _ _ _ _ _'''''''''''''''''' '__objclass__' ' '' __reduce__ '', '' __reduce_ex__ '', '' __repr__',

'' __self__ '', '' __setattr__ '', '' __sizeof__ '',' '__str__'',

''_ _ subclasshook__'']


>> X .__ lt __.__ func__



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

文件"< stdin>",第1行,i n< module>

AttributeError:''method-wrapper''对象没有属性''__func__''


Michael Foord

-
http://www.ironpythoninaction.com/

解决方案

Fuzzyman< fu ****** @ gmail.comwrote:


那么如何判断X .__ lt__是否从对象继承?我可以看看



我没有安装python 2.6所以我不能尝试但我认为可以

工作是:


>> class Foo(object):



.... def hello(self):

....传递

.. ..


>> class Bla(Foo):



.... def hello(self):

....传递

。 ...


>> class Baz(Foo):



....传递

....


>> Baz.hello.im_func



< function hello at 0x2b1630>


>> Bla.hello.im_func



< function hello at 0x2b1670>


>> Foo.hello.im_func



< function hello at 0x2b1630>


>> Foo.hello.im_func is Baz.hello.im_func



True


>> Foo.hello.im_func是Bla.hello.im_func



False


对我来说也有意义。如果它是继承的,我希望它是

与其中一个基础相同的函数对象(你可以用它来获得

inspect.getmro(Clazz)) 。另一方面,如果你覆盖它,它不是

相同的函数对象,所以当应用于''是'时它不会返回True。


HTH


-

Valentino Volonghi又名Dialtone
http://stacktrace.it - Aperiodico di resistenza informatica

Blog: http://www.twisted.it/

公共测试版: http://www.adroll.com/


Fuzzyman写道:


大家好,


我可能很愚蠢(以前发生过),但我很挣扎
用Python 2.6修复一些代码破坏。


我有一些代码在类上查找''__lt__''方法:


if hasattr(clr,''__ lt__''):


但是 - 在Python 2.6对象中已经增加了一个默认的实现

''__ lt__'',所以这个测试总是返回True。


>>>类X(对象):传递



...


>>> X .__ lt__


$ b 0xa15cf0>类型为对象的$ b< method-wrapper''__ lt__''


>>> X .__ lt__ == object .__ lt__



错误


那么如何判断X .__ lt__是否从对象继承?我可以在班级的__dict__中查看

- 但是这并不能告诉我是否从基类继承了''__lt__''除了对象。 (看里面

方法包装器repr与正则表达式是不可接受的答案...)



他们是不同类型的。我不确定你能用多少钱,

或它有多可靠。


>>类A(对象):



....传递

....


>> class B(对象):



.... def __lt __(self,other):

....传递

....


>> hasattr (A,''__ lt__'')



True


>> hasattr(B,''__ lt__'')



True


>> ; A .__ lt__



< method-wrapper''__lt__''类型为0x00B81D48>


>> B .__ lt__



< unbound method B .__ lt __>


>>>


10月5日,8:15 * pm,dialUAZ ### UZ#


Hello all,

I may well be being dumb (it has happened before), but I''m struggling
to fix some code breakage with Python 2.6.

I have some code that looks for the ''__lt__'' method on a class:

if hasattr(clr, ''__lt__''):

However - in Python 2.6 object has grown a default implementation of
''__lt__'', so this test always returns True.

>>class X(object): pass

....

>>X.__lt__

<method-wrapper ''__lt__'' of type object at 0xa15cf0>

>>X.__lt__ == object.__lt__

False

So how do I tell if the X.__lt__ is inherited from object? I can look
in the ''__dict__'' of the class - but that doesn''t tell me if X
inherits ''__lt__'' from a base class other than object. (Looking inside
the method wrapper repr with a regex is not an acceptable answer...)

Some things I have tried:

>>X.__lt__.__self__

<class ''__main__.X''>

>>dir(X.__lt__)

[''__call__'', ''__class__'', ''__cmp__'', ''__delattr__'', ''__doc__'',
''__format__'', ''__getattribute__'', ''__hash__'', ''__init__'', ''__name__'',
''__new__'', ''__objclass__'', ''__reduce__'', ''__reduce_ex__'', ''__repr__'',
''__self__'', ''__setattr__'', ''__sizeof__'', ''__str__'',
''__subclasshook__'']

>>X.__lt__.__func__

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: ''method-wrapper'' object has no attribute ''__func__''

Michael Foord
--
http://www.ironpythoninaction.com/

解决方案

Fuzzyman <fu******@gmail.comwrote:

So how do I tell if the X.__lt__ is inherited from object? I can look

I don''t have python 2.6 installed so I can''t try but what I think could
work is:

>>class Foo(object):

.... def hello(self):
.... pass
....

>>class Bla(Foo):

.... def hello(self):
.... pass
....

>>class Baz(Foo):

.... pass
....

>>Baz.hello.im_func

<function hello at 0x2b1630>

>>Bla.hello.im_func

<function hello at 0x2b1670>

>>Foo.hello.im_func

<function hello at 0x2b1630>

>>Foo.hello.im_func is Baz.hello.im_func

True

>>Foo.hello.im_func is Bla.hello.im_func

False

Which to me also makes sense. If it''s inherited I expect it to be the
very same function object of one of the bases (which you can get with
inspect.getmro(Clazz)). On the other end if you override it it''s not the
same function object so it won''t return True when applied to ''is''.

HTH

--
Valentino Volonghi aka Dialtone
http://stacktrace.it -- Aperiodico di resistenza informatica
Blog: http://www.twisted.it/
Public Beta: http://www.adroll.com/


Fuzzyman wrote:

Hello all,

I may well be being dumb (it has happened before), but I''m struggling
to fix some code breakage with Python 2.6.

I have some code that looks for the ''__lt__'' method on a class:

if hasattr(clr, ''__lt__''):

However - in Python 2.6 object has grown a default implementation of
''__lt__'', so this test always returns True.

>>>class X(object): pass

...

>>>X.__lt__

<method-wrapper ''__lt__'' of type object at 0xa15cf0>

>>>X.__lt__ == object.__lt__

False

So how do I tell if the X.__lt__ is inherited from object? I can look
in the ''__dict__'' of the class - but that doesn''t tell me if X
inherits ''__lt__'' from a base class other than object. (Looking inside
the method wrapper repr with a regex is not an acceptable answer...)

They''re of different types. I''m not sure how much you could use that,
or how reliable it is.

>>class A( object ):

.... pass
....

>>class B( object ):

.... def __lt__( self, other ):
.... pass
....

>>hasattr( A, ''__lt__'' )

True

>>hasattr( B, ''__lt__'' )

True

>>A.__lt__

<method-wrapper ''__lt__'' of type object at 0x00B81D48>

>>B.__lt__

<unbound method B.__lt__>

>>>


On Oct 5, 8:15*pm, dialUAZ###UZ#


这篇关于Python 2.6:确定方法是否被继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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