Pointers in Python

A

Anton Shishkov

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'>
 
C

Chris Rebert

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
 
L

Laszlo Nagy

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.
Please read about "mutable and immutable objects in Python". If you
understand the difference between them, you will get the idea. I'll
explain program anyway, showing you need to think differently.
Code:
def add(b):

b = {}
Here you create a new dict objects, and rebind the local name 'b' to
this newly created object.
print type(b)

a = []
Here you create a new list object, and bind the global name 'a' to this
object.
print type(a)
add(a)
You call your function here, passing the list object you have previously
created. The list object won't be changed (and in fact, won't be used at
all).
print type(a)
Now, if try to rewrite your example like this:

def add(b):
b.append(10) # Call append() on b -> change its state!

a = []
print a
add(a)
print a # Prints [10]


Look how the list object was changed.

If you use an immutable object, it is totally different:


def add(b):
b = b + 2

a = 10
print a
add(a)
print a # Prints 10


It is because inside the add() function, you do not CHANGE the state of the integer object. Instead of that, you create a new integer object, and rebind that to a local variable name. State of certain objects cannot be changed at all. They are called immutable. Integers, floats, strings are immutable. Other objects are mutable: lists, dicts etc.

This is a big difference between C and Python. In C, variable names are just pointers to memory locations. In Python, they are references to objects.

I'm sure others will also answer your question...

Best,

Laszlo
 
B

Bruno Desthuilliers

Anton Shishkov a écrit :
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.

(snip)

Others already answered on this. Now, the real question is : why to you
want to do such a thing ?

Of one the most common use case for this idiom in C is to workaround the
lack of multiple return values, so your function returns an error code
*and* "sets" a value thru the pointer. This idiom makes no sense in
Python since we have exception handling. FWIW, other use case are
usually solved by tuple unpacking, ie:


def fun(a, b):
return a+b, a*b

sum_, prod = fun(5, 4.5)


HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,174
Messages
2,570,940
Members
47,486
Latest member
websterztechnologies01

Latest Threads

Top