How to deal __getattr__

limodou limodou at gmail.com
Sat Oct 16 08:14:09 CEST 2004


I resolved the problem via defining __eq__method, just like:

>>> class A:
	def __getattr__(self, name):
		print name
		return None
	def __eq__(self, obj):
		return id(self) == id(obj)

	
>>> a=A()
>>> b=A()
>>> a==b
False

I don't know whether it's the right solution, any ideas?

On Sat, 16 Oct 2004 13:59:11 +0800, limodou <limodou at gmail.com> wrote:
> I found it puzzled me that:
> 
> class A:
>    def __getattr__(self, name):
>        return None
> a=A()
> b=A()
> a==b will raise Exception:
> 
> __eq__
> 
> Traceback (most recent call last):
>  File "<pyshell#7>", line 1, in -toplevel-
>    a==b
> TypeError: 'NoneType' object is not callable
> 
> I want to compare the objects themself not the attributes of them. But
> it seems python invoke __getattr__ method, it's strange. How can I
> compare the object directly without calling __getattr__?
> 
> Thankx
> 
> --
> I like python!
> 


-- 
I like python!



More information about the Python-list mailing list