Re: Mutability of function arguments?
- From: bruno at modulix <onurb@xxxxxxxxxxx>
- Date: Thu, 08 Dec 2005 10:41:08 +0100
ex_ottoyuhr wrote:
> I'm trying to create a function that can take arguments, say, foo and
> bar, and modify the original copies of foo and bar as well as its local
> versions -- the equivalent of C++ funct(&foo, &bar).
This is already what you have. In Python, all you have are references to
objects, there is no "local version".
> I've looked around on this newsgroup and elsewhere, and I gather that
> this is a very common concern in Python, but one which is ordinarily
> answered with "No, you can't. Neat, huh?"
Pardon ???
>>> def appendToList(aList, aValue):
.... aList.append(aValue)
....
>>> mylist = range(10)
>>> mylist
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> appendToList(mylist, 42)
>>> mylist
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 42]
>>>
Now the usual considerations apply :
- rebinding an arg has of course only local effect
- immutable objects are still immutables
Also note that since
1/ Python as a good support for exception handling
2/ a function can return multiple values [1],
there is less need for such constructs than in C or C++.
[1] the truth is that the function can return a unique tuple, that can
be unpacked as any other.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb@xxxxxxxxxxx'.split('@')])"
.
- References:
- Mutability of function arguments?
- From: ex_ottoyuhr
- Mutability of function arguments?
- Prev by Date: Re: Is Python string immutable?
- Next by Date: Re: Bitching about the documentation...
- Previous by thread: Re: Mutability of function arguments?
- Next by thread: hi i have some doubts on a volume and the file system
- Index(es):
Relevant Pages
|
|