[Tutor] string immutability

Alan Gauld alan.gauld at btinternet.com
Tue Oct 25 01:56:56 CEST 2011


On 24/10/11 20:52, Johan Martinez wrote:

> Also, is there any doc link where I can find all the information about
> String object - class and instance methods.


 >>> help(str)

or even

 >>> help("")

For a quick list try dir()

 >>> dir ("")


> Finally I figured it out ( __length__() ) thanks to ipython shell env.

len(x)

gets converted to x.__length__() by Python.
So you can make any object work with len() by providing
a __length__() method. Similarly you can make any object into a string 
by providing a __str__() metjod and then str(obj) will work (as will 
print (obj) )

There are a bunch of these "magic" methods that you can provide to help 
make your objects appear more like the standard built in objects.

> But is there any online documentation or interactive reference like
> ruby-ri?

There are various help browsers for Python.
The one that comes with ActiveState Python for Windows is quite good 
IMHO and integrates with Windows Help too.

But personally I just use dir() and help() and Google...



-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list