FAQ

Tom Plunket wrote:
I want to do something along the lines of the following C++ code:

void change(int& i)
{
i++;
}


Is there any way to do references like this in Python? It'd be
like this, though clearly this doesn't change the thing I want it
to change:

def change(val):
val += 1

v = 0
change(v)

# v == 1 now.

thanks.

I can see that maybe if I passed in a list of one integer I could
change that, but that seems circuitous to me.


-tom!
Hi Tom,
Welcome to Python.
I guess the first thing to remember is that Python is not any other
language. It has its own ways of doing things, and its own nomenclature,
that while in some cases may use the same term or phrase as another
language, or symbols used in Mathematics - It puts a Python meaning on them.

Python does things its own way. I found that I got much more productive
when I learnt enough to let go of the C idioms in my case that I had
initially. I guess I was helped in that I was a good programmer in C,
didn't like C++ or Java and was looking for another language that would
be more productive. I could do the C pointer wizardry but in no way did
I think that it was ideal. It sometimes helps to have an understanding
of what Python is doing 'under the hood', but I don't solve my Python
problems by translating solutions expressed in C pointer terms.

Other mails have gone into the minutia of trying to answer the question.
I would just like to add that if you look up scope rules or the global
keyword you will find ways of accessing variables defined outside of a
function definition.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

People

Translate

site design / logo © 2022 Grokbase