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__'
Hmmm... I can get this working with Python 2.6 with:
if '__lt__' in dir(cls):
The default implementation of '__lt__' doesn't appear in the dir of
classes. However this fails with Python 3 where the default
implementation *does* appear in the output of 'dir'. Any suggestions?
Michael Foord
-- http://www.ironpythoninaction.com/ 8 2585
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...)
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__'
Hmmm... I can get this working with Python 2.6 with:
if '__lt__' in dir(cls):
The default implementation of '__lt__' doesn't appear in the dir of
classes. However this fails with Python 3 where the default
implementation *does* appear in the output of 'dir'. Any suggestions?
Methods are objects. How do you know if two references refer to the
same object? You use "is":
X.__lt__ is object.__lt__
On Oct 5, 7:13*pm, MRAB <goo...@mrabarnett.plus.comwrote:
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.
Hmmm... I can get this working with Python 2.6 with:
Methods are objects. How do you know if two references refer to the
same object? You use "is":
X.__lt__ is object.__lt__
That doesn't work for me.
>>class A( object ):
.... pass
....
>>class B( A ):
.... def __lt__( self, other ):
.... return self
....
>>a= A() b= B() B.__lt__ is object.__lt__
False
>>A.__lt__ is object.__lt__
False
>>>
Further, it's been noted before that
A().meth is not A().meth
On Oct 6, 1:13 am, MRAB <goo...@mrabarnett.plus.comwrote:
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...)
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__'
Hmmm... I can get this working with Python 2.6 with:
if '__lt__' in dir(cls):
The default implementation of '__lt__' doesn't appear in the dir of
classes. However this fails with Python 3 where the default
implementation *does* appear in the output of 'dir'. Any suggestions?
Methods are objects. How do you know if two references refer to the
same object? You use "is":
X.__lt__ is object.__lt__
Didn't you see that even an equality test fails - so they are not the
same (that being the problem)...
They are unbound method objects - in Python 3 the unbound method has
gone away, so the problem is with Python 2.6.
Michael
-- http://www.ironpythoninaction.com/
On Oct 6, 4:30*am, Fuzzyman <fuzzy...@gmail.comwrote:
On Oct 6, 1:13 am, MRAB <goo...@mrabarnett.plus.comwrote:
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...)
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__'
Hmmm... I can get this working with Python 2.6 with:
if '__lt__' in dir(cls):
The default implementation of '__lt__' doesn't appear in the dir of
classes. However this fails with Python 3 where the default
implementation *does* appear in the output of 'dir'. Any suggestions?
Methods are objects. How do you know if two references refer to the
same object? You use "is":
X.__lt__ is object.__lt__
Didn't you see that even an equality test fails - so they are not the
same (that being the problem)...
They are unbound method objects - in Python 3 the unbound method has
gone away, so the problem is with Python 2.6.
Michael
--http://www.ironpythoninaction.com/
Not tested extensively.
class NoLTException( Exception ): pass
class NoLT( object ):
def __lt__( self, other ):
raise NoLTException()
class A( NoLT ):
pass
class B( A ):
def __lt__( self, other ):
return self
def test_lt( obj ):
try:
obj.__lt__( None )
except NoLTException:
return False
except:
pass
return True
>>a= A() b= B() test_lt( a )
False
>>test_lt( b )
True
>>>
This method won't work for arbitrary classes, only ones that you
control, that inherit from 'NoLT'. The 'test_lt' function works by
trying to call '__lt__' on its argument. The parameter to it doesn't
matter because of what happens next. If '__lt__' raises a
NoLTException, you know it was inherited from NoLT. Otherwise, even
if another exception occurs, the object you know has '__lt__'.
It's a very object oriented solution. Essentially you're inheriting
all the classes that you want to fail, from a class that does.
On Oct 6, 7:01*pm, "Aaron \"Castironpi\" Brady" <castiro...@gmail.com>
wrote:
On Oct 6, 4:30*am, Fuzzyman <fuzzy...@gmail.comwrote:
On Oct 6, 1:13 am, MRAB <goo...@mrabarnett.plus.comwrote:
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...)
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__'
Hmmm... I can get this working with Python 2.6 with:
if '__lt__' in dir(cls):
The default implementation of '__lt__' doesn't appear in the dir of
classes. However this fails with Python 3 where the default
implementation *does* appear in the output of 'dir'. Any suggestions?
Methods are objects. How do you know if two references refer to the
same object? You use "is":
X.__lt__ is object.__lt__
Didn't you see that even an equality test fails - so they are not the
same (that being the problem)...
They are unbound method objects - in Python 3 the unbound method has
gone away, so the problem is with Python 2.6.
Michael
--http://www.ironpythoninaction.com/
Not tested extensively.
class NoLTException( Exception ): pass
class NoLT( object ):
* * def __lt__( self, other ):
* * * * * * raise NoLTException()
class A( NoLT ):
* * pass
class B( A ):
* * def __lt__( self, other ):
* * * * * * return self
def test_lt( obj ):
* * try:
* * * * * * obj.__lt__( None )
* * except NoLTException:
* * * * * * return False
* * except:
* * * * * * pass
* * return True
>a= A() b= B() test_lt( a )
False
>test_lt( b )
True
This method won't work for arbitrary classes, only ones that you
control, that inherit from 'NoLT'. *The 'test_lt' function works by
trying to call '__lt__' on its argument. *The parameter to it doesn't
matter because of what happens next. *If '__lt__' raises a
NoLTException, you know it was inherited from NoLT. *Otherwise, even
if another exception occurs, the object you know has '__lt__'.
It's a very object oriented solution. *Essentially you're inheriting
all the classes that you want to fail, from a class that does.
But not a very good solution to the problem...
The specific problem is to determine if an arbitrary class implements
a specified comparison method. The general problem (that gives rise to
the specific problem) is to write a class decorator that can implement
all comparison methods from a class that implements only one.
See: http://code.activestate.com/recipes/576529/
Michael
-- http://www.ironpythoninaction.com/
On Oct 6, 1:17*pm, Fuzzyman <fuzzy...@gmail.comwrote:
On Oct 6, 7:01*pm, "Aaron \"Castironpi\" Brady" <castiro...@gmail.com>
wrote:
It's a very object oriented solution. *Essentially you're inheriting
all the classes that you want to fail, from a class that does.
But not a very good solution to the problem...
The specific problem is to determine if an arbitrary class implements
a specified comparison method. The general problem (that gives rise to
the specific problem) is to write a class decorator that can implement
all comparison methods from a class that implements only one.
See:http://code.activestate.com/recipes/576529/
Michael
--http://www.ironpythoninaction.com/
Nope, I'm out of ideas, I'm afraid.
2008/10/5 Fuzzyman <fu******@gmail.com>:
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...)
I don't have Python 2.6 available, but if __lt__ on it works similarly
as __str__ on Python 2.5, you might be able to achieve this either
with inspect.ismethod or by checking methods' im_class attribute
directly:
>>class C(object):
.... pass
....
>>class D(object):
.... def __str__(self):
.... return ''
....
>>class E(D):
.... pass
....
>>import inspect inspect.ismethod(C().__str__)
False
>>inspect.ismethod(D().__str__)
True
>>inspect.ismethod(E().__str__)
True
>>> C().__str__.im_class
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'method-wrapper' object has no attribute 'im_class'
>>D().__str__.im_class
<class '__main__.D'>
>>E().__str__.im_class
<class '__main__.E'>
Cheers,
.peke
2008/10/7 Pekka Laukkanen <pe**@iki.fi>:
2008/10/5 Fuzzyman <fu******@gmail.com>:
>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...)
I don't have Python 2.6 available, but if __lt__ on it works similarly
as __str__ on Python 2.5, you might be able to achieve this either
with inspect.ismethod or by checking methods' im_class attribute
directly:
>>>class C(object):
... pass
...
>>>class D(object):
... def __str__(self):
... return ''
...
>>>class E(D):
... pass
...
>>>import inspect inspect.ismethod(C().__str__)
False
>>>inspect.ismethod(D().__str__)
True
>>>inspect.ismethod(E().__str__)
True
>>>> C().__str__.im_class
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'method-wrapper' object has no attribute 'im_class'
>>>D().__str__.im_class
<class '__main__.D'>
>>>E().__str__.im_class
<class '__main__.E'>
Ooops, didn't notice this was suggested already. One more attempt,
hopefully this is unique. =)
>>C().__str__.__objclass__
<type 'object'>
>>D().__str__.__objclass__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute '__objclass__'
>>'spam'.__str__.__objclass__
<type 'str'>
Someone who actually knows what __objclas__ does can probably comment
does this make any sense in your case.
Cheers,
.peke This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Andrew Dalke |
last post by:
Is there an author index for the new version of the
Python cookbook? As a contributor I got my comp version
delivered today and my ego wanted some gratification.
I couldn't find my entries.
...
|
by: Matthias Kaeppler |
last post by:
Hi,
sorry for my ignorance, but after reading the Python tutorial on
python.org, I'm sort of, well surprised about the lack of OOP
capabilities in python. Honestly, I don't even see the point at...
|
by: Xah Lee |
last post by:
Python, Lambda, and Guido van Rossum
Xah Lee, 2006-05-05
In this post, i'd like to deconstruct one of Guido's recent blog about
lambda in Python.
In Guido's blog written in 2006-02-10 at...
|
by: Fuzzyman |
last post by:
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:
...
|
by: Fuzzyman |
last post by:
Hello all,
Sorry - my messages aren't showing up via google groups, so I'm kind
of posting on faith...
Anyway, I solved my problem (I think)...
import sys
if sys.version_info == 3:
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |