Pointers in Python

Chris Rebert clp2 at rebertia.com
Tue Apr 27 15:19:53 CEST 2010


On Tue, Apr 27, 2010 at 6:09 AM, Anton Shishkov
<anton.shishkov at gmail.com> wrote:
> Hi, I can't figure out how can I change the variable type in function.
> In C I could do that easily by changing pointer.
>
> Code:
> def add(b):
>
>    b = {}
>    print type(b)
>
> a = []
> print type(a)
> add(a)
> print type(a)
>
> Output:
> <type 'list'>
> <type 'dict'>
> <type 'list'>

Python uses call-by-object
(http://effbot.org/zone/call-by-object.htm), so what you're trying to
do isn't possible.
One would normally just return the new object as a result instead.
If you have a more concrete example, someone might be able to suggest
a better approach.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list