class __getitem__ when item is not a sequence ???

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Sat Apr 21 15:32:25 EDT 2007


cfriedalek at gmail.com a écrit :
> Sorry for the vague subject. Not sure what the right terminology is.
> 
> How can I use an instance's data by reference to the instance name,
> not the instance attribute? OK the question is probably really poor
> but hopefully an example will make it clear.
> 
> 
>>x=1
>>type(x)
> 
> <type 'int'>
> 
>>x.__add__(1)
> 
> 2
> 
>>print x
> 
> 1
> 
>>3*x
> 
> 3
> 
> In this case x is an integer. My understanding is that x in an
> instance of an integer class. Since it refers to only a single value
> things like print x, 3*x etc operate on the instance name which seems
> to refer to the instance data, not the instance itself. I want to do
> the same for my own classes.

http://docs.python.org/ref/specialnames.html



More information about the Python-list mailing list