Hi Sébastien, and welcome.

On Wed, Sep 25, 2019 at 04:15:58PM +0700, Sébastien Eskenaz wrote:
> Hello,
> 
> Several libraries have complex objects but no comparison operators for them
> other than "is" which checks if we are comparing an object with itself.

Not true: equality ``==`` is also defined for all objects. By default, 
``==`` falls back to testing for object identity, but your classes can 
define ``__eq__`` to override that.

> It would be quite nice to be able to compare any two objects together.

Compare in what way?


> I made this function in python to have a starting point
> https://gist.github.com/SebastienEske/5a9c04e718becd93b7928514e80f0211

I don't have much time today, but I had a quick look. Your function 
begins with:

        if type(obj) != type(obj1):
            return False

That means that an instance of a subclass and an instance of 
its parent class will always compare False. So:

    class MyStr(str): pass
    x = MyStr("hello")
    compare_objs("hello", x)  # returns False

Why is this useful?

Then your code says:

    for a in dir(obj):


You should not use ``dir`` like that, it is not designed for 
programmatic use. The docs warn 

Because dir() is supplied primarily as a convenience for use at an 
interactive prompt, it tries to supply an interesting set of names more 
than it tries to supply a rigorously or consistently defined set of 
names, and its detailed behavior may change across releases.

https://docs.python.org/3/library/functions.html#dir

I don't have time to go through the rest of your function now, but I 
think you should explain what you expect "compare any two objects" to do 
in words, rather than expect that we read the source code and try to 
guess.

Thanks,



-- 
Steven
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/2RDWJCLUNGXTS6R3NTE64FXJYF5GOZNB/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to