A interesting difference between classic and new style objects

Oren Tirosh oren-py-l at hishome.net
Mon Jan 7 03:33:36 EST 2002


>>> class Classic:
... 	def __repr__(self):
... 		return 'original method'
... 	
>>> c=Classic()
>>> repr(c)
'original method'
>>> c.__repr__ = lambda: 'override'
>>> repr(c)
'override'
>>> c.__repr__()
'override'

>>> class NewStyle(object):
... 	def __repr__(self):
... 		return 'original method'
... 	
>>> n=NewStyle()
>>> repr(n)
'original method'
>>> n.__repr__ = lambda: 'override'
>>> repr(n)
'original method'		# <- here's the difference
>>> n.__repr__()
'override'

With a new style objects built-in functions such as repr, iter etc. see the 
original method with which the object was born even after it is overridden 
by assignment.  

Is this a bug or a feature?

	Oren




More information about the Python-list mailing list