An interesting difference between classic and new style objects

Pekka Pessi Pekka.Pessi at nokia.com
Mon Jan 7 13:53:35 EST 2002


In message <mailman.1010409010.25803.python-list at python.org> Oren Tirosh <oren-py-l at hishome.net> writes:
>> As here we are indeed dealing with "assignment to [an item] of
>> a __dict__", we may be in the grey zone to "be considered
>> off-limits".  

	Assignment to an item of a __dict__ would look like this:

n.__dict__['__repr__'] = lambda: 'override'

>>Some clarification would surely be welcome.

>In this case __repr__ is not a special attribute.  It is a user attribute
>with a well-known name that some builtin functions refer to.  There are even 
>examples of this happening for attributes without underscores. 

	IMO, __repr__ is not an special attribute.

>There is no use of the introspection API, either.  I believe that assignment 
>of attributes is well within the bounds of the basic language definition and
>yet it produces different results for classic and new style objects.

	You should keep in mind difference between a method and an
	attribute. The language reference talks about special methods
	(like __repr__ and __call__); unfortunately, the current
	implementation tests for attributes for hysterical raisins. 
	There are tons of old code that expect __call__ attribute to
	work. Also, some code assumes that for every instance i, type(i)
	is InstanceType.

					Pekka
	



More information about the Python-list mailing list