print "hello", >> file

Steve Holden sholden at holdenweb.com
Tue Feb 25 14:49:45 EST 2003


"phil hunt" <philh at cabalamat.uklinux.net> wrote ...
> On Tue, 25 Feb 2003 12:04:14 -0500, Peter Hansen <peter at engcorp.com>
wrote:
> >phil hunt wrote:
> >>
> >> Wouldn't it be nice if appending to a file, appending to a string
> >> and appending to stdout had the same syntax?
> >>
> >>    f = file("somefilename", "w")
> >>    f << "hello"
> >>
> >>    s = "some string"
> >>    s << "hello"
> >>
> >>    out << "hello"
> >
> >Since you can't append to a string,
>
> But I can:
>
>    philh:~> python
>    Python 2.0 (#1, Jan 19 2001, 17:54:27)
>    [GCC 2.95.2 19991024 (release)] on linux2
>    Type "copyright", "credits" or "license" for more information.
>    >>>
>    >>>
>    >>> s = "some string"
>    >>> s += "hello"
>    >>> s
>    'some stringhello'
>

No, you can't mute the immutable:

>>> s = "somestring"
>>> s1 = s
>>> s += "misconception"
>>> s
'somestringmisconception'
>>> s1
'somestring'

But:

>>> lst = [1, 2, 3]
>>> lst1 = lst
>>> lst += [4]
>>> lst
[1, 2, 3, 4]
>>> lst1
[1, 2, 3, 4]

Examine:

>>> id(s), id(s1), id(lst), id(lst1)
(168393208, 168392352, 168393824, 168393824)

regards
--
Steve Holden                                  http://www.holdenweb.com/
Python Web Programming                 http://pydish.holdenweb.com/pwp/
Register for PyCon now!            http://www.python.org/pycon/reg.html







More information about the Python-list mailing list