[ python-Bugs-871378 ] __getattr__ and __add__ collides

SourceForge.net noreply at sourceforge.net
Mon Jan 5 21:12:39 EST 2004


Bugs item #871378, was opened at 2004-01-06 02:12
Message generated for change (Tracker Item Submitted) made by Item Submitter
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=871378&group_id=5470

Category: Python Interpreter Core
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Huang Guang Shen (shhgs)
Assigned to: Nobody/Anonymous (nobody)
Summary: __getattr__ and __add__ collides

Initial Comment:
My English is not good enough, so I'd rather use code :

class A :
	def __init__(self) :
		self.__content = [] 
	def __add__(self, x) :
		self.__content.append(x)
	def __getattr__(self, attrname) :
		if string.upper(attrname) == "FIRST" :
			return self.__content[0]
		elif string.upper(attrname) == "LAST" :
			return self.__content[-1]

>>> a = A()
>>> a + 12

And it complains.

but when I substitute the __add__ with add(), it seems
right.

class A :
	def __init__(self) :
		self.__content = []
	def add(self, x) :
		self.__content.append(x)
	def __getattr__(self, attrname) :
		if string.upper(attrname) == "FIRST" :
			return self.__content[0]
		elif string.upper(attrname) == "LAST" :
			return self.__content[-1]

>>> a = A()
>>> a.add(12)
>>> a.add(16)
>>> a.first
12
>>> a.last
16

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=871378&group_id=5470



More information about the Python-bugs-list mailing list