append

James T. Dennis jadestar at idiom.com
Fri May 25 03:12:26 EDT 2007


Bruno Desthuilliers <bdesth.quelquechose at free.quelquepart.fr> wrote:
> HMS Surprise a ?crit :
>> Trying not to be a whiner but I sure have trouble finding syntax in
>> the reference material. I want to know about list operations such as
>> append. 

> The only thing you have to know is that it doesn't exists. Python 
> strings are immutables. If you want to sequentially build a string, you 
> can either rebind the name to a new string :

 Huh?  Where did strings come into this question?

> s = ""
> for c in "abcdef":
>   s += c

 Huh?  Why not just use:  s = list(s)?  For a more clear example:

	>>> list("foo")
	['f', 'o', 'o']

 See? 

> or collect substrings in a list and join it:

> s = []
> for c in "abcdef":
>   s.append(c)
> s = "".join(s)

 Yes, "".join(list("foo")) is a sort of "expensive" no-op.

 As for the original post:  dir() and help() from the interactive
 prompt are good friends.  An ipython prompt is an even better 
 friend to have while learning python.  Consider this:

 	In[1]:list.app[[Tab]] ...
        list.append

	In[1]:list.append[[?]][[Enter]]

	Type:           method_descriptor
	Base Class:     <type 'method_descriptor'>
	String Form:    <method 'append' of 'list' objects>
	Namespace:      Python builtin
	Docstring:
	    L.append(object) -- append object to end

	In[2]:

 ... where I'm highlighting some keystrokes with [[]] marks.

 The point is that ipython offers [Tab] completion(*) and
 has a number other cool features.  For example if you end a
 function/method/class/member line with a ? key and hit enter,
 then you get a help screen as shown in my mini-transcript above.

 Python simply has the best interactive introspection features
 around.  (Also, if you use docstrings in your own code than
 these features will work on your code, too).

 For extra fun go into one of your directories which contains
 some of your .py files, and/or add a few such directories to
 your PYTHONPATH environment variable.  Then start up a command
 like:

	pydoc -p 8080

 ... or pick some other TCP port.

 Then point a web browser at localhost:8080 ...

 ... see?  There's a mini-web service with clickable links
 to read the docs for all the Python modules installed on
 your system ... including your own code!

 * ([Tab]-completion is also possible in the normal python >>> 
 interpreter using: import rlcompleter
 ... and then calling the relatively obscure incantation:

	rlcompleter.readline.parse_and_bind("tab: complete")

 If, like me you prefer vi-mode readline editing then you
 add another incantation: 

	rlcompleter.readline.parse_and_bind("set editing-mode vi")

 ... or you can simply put the following in you ~/.inputrc:

	$if python
	set editing-mode vi
	$endif

  ... and, in that case, you can bind other keystrokes into macro
 strings like: C-o:"import sys,os" or whatever).


-- 
Jim Dennis,
Starshine: Signed, Sealed, Delivered




More information about the Python-list mailing list