W
W
warchant2015-06-18 17:12:44
Python
warchant, 2015-06-18 17:12:44

How to compare objects with each other in Python?

It sounds silly, but I need to write a function that takes one argument as input and returns something that is then compared to math and re . Don't ask questions why, just tell me how to do it :) Thank you.

def func(anything):
    return something

import math
import re

# оба должны быть True
func(re) <= re 
func(re) >= math

If you just return the input argument, then this is what happens:
TypeError: unorderable types: module() >= module()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2015-06-18
@warchant

The returned object must have __ge__ and __le__ methods

class A(object):
    def __ge__(self, other):
        return True

    def __le__(self, other):
        return True

You can add an if other.__name__ in ('re', 'math') condition if the comparison is to other objects.
Good luck with the shitcode :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question