How to determine what called __cmp__ method?

Pearu Peterson pearu at ioc.ee
Fri Oct 20 17:03:20 EDT 2000


Hi, 

I have the following situation:

>>> foo(1) < foo(2)

where 

class foo:
	def __init__(self,i):
		self.i = i
	def __cmp__(self,obj):
		# who called me???
		return self.i<obj.i or -(self.i>obj.i) or 0

The question is how to determine inside __cmp__ method where it was
called? That is I want to know was it called for evaluating
either inequality '<', or '>', or '<=', or ...?

I know how to determine the caller if the caller is Python function (I
learned it from Python snippets) but if the caller is built-in function,
then this method does not work. The function is

def getcaller():
    try: raise RuntimeError
    except RuntimeError:
        exc, val, tb = sys.exc_info()
        frame = tb.tb_frame.f_back
        del exc, val, tb
    try:
        ret = frame.f_back.f_code.co_name
        return ret
    except AttributeError:  # called from the top
        return None

Thanks,
	Pearu




More information about the Python-list mailing list