faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

2 of 3 people (67%) answered Yes
Recently 1 of 2 people (50%) answered Yes

Entry

Mutable function arguments (was: Interesting thread...)

Jul 5th, 2000 10:02
Nathan Wallace, Hans Nowak, Snippet 224, ze5yr


"""
Packages: basic_datatypes.lists
"""
"""
In article <Pine.GSO.4.10.9908131047510.3761-100000@crusoe.crusoe.net>,
  japhy@pobox.com wrote:
> I wish I could change a string in-place with a function.*
> [bobbit]
> * meaning:
>
> def func(str):
>   # something modifying str
>
> str = "foo"
> func(str)
> print str # prints... "bar", for example
Enclose it in a list, which is mutable:
"""
def func(l):
  l[0]="bar"
l=["foo"]
func(l)
print l
# ['bar']