A bundle of questions from a Python newbie

Gustavo Cordova gcordova at hebmex.com
Wed Feb 20 17:56:31 EST 2002


> 
> > 
> > 6. Is there a reason why 3.__abs__() does not work? After 
> all 3 is an
> > object (in the wider sense of the word). And it does work 
> if we assign
> > it to a variable, eg, 
> > 
> > var = 3
> > var.__abs__()
> > 
> > I know that code like the above (methods applied to number 
> literals) is
> > probably rare, but consistence is also a good thing.
> > 
> 
> because not everything is an object (yet)
> 

But, it's because the "3." is being interpreted
as a floating point number. Use a space:

>>> 3 . __abs__()
3
>>>


> > 10. I have a class that works like a list in the sense that supports the
> > method __getitem__ and its relatives. But I'm at a loss as to what I
> > have to do to support the slice notation.
> 
> >>> class Foo:
> ...   def __getslice__(*args):
> ...     print args
> ... 
> >>> f = Foo()
> >>> f[1:1]
> (<__main__.Foo instance at 0x80905e4>, 1, 1)
> 

and ditto for:

...   def __setslice__(*args): print args
...   def __delslice__(*args): print args


-gustavo




More information about the Python-list mailing list