[Tutor] pointers for python?

Alan Gauld alan.gauld at blueyonder.co.uk
Sun Oct 5 18:38:51 EDT 2003


> under C, you'd have to pass a pointer to A in order to modify it,
> correct?

It is one solution but is not even good practice in C. The reason
why it's sometimes necessary is that C can only return a single value
from a function so where multiple return values might be needed
(like scanf say) then passing pointers (or references in C++) is
the only solution.

But C (unlike Pascal or ADA) does not have a "procedure" construct
only a functuion  - which by definition has a return value - and
pre ANSI didn't even encourage void returns, so that all the
standard library functions return a value (even printf - the
number of characters printed)

So that:

> table = gtk.Table(3,2,gtk.FALSE)
> fill_table(table,["Label 1 text","label 2 text","Label 3 text"])

Is not good C style either(you are ignoring the return value of
fill_table), its just unfortunately one that has become common.
(This is why tools like lint will complain about printf() statements
where the return value is not used.)

> table = fill_table(table,["Label 1 text","label 2 text","Label 3
text"])

This is good style and practice from a computing science point of
view regardless of language. Its a matter of improving reliability
and reusability if functions don't have side effects - and that
includes changing the parameters passed to them.

> As i said in an earlier mail, the question was purely one of
> discovery, which arose when comparing C++ and python code ;)

Incidentally, this is the first time you mentioned C++ instead of C.
C++ introduced the concept of references specifically to help deal
with this issue and reduce the need to pass pointers.

Thus in C++ it is considered preferable to define:

void foo(int& i){...}

to

void foo(int* ip){...}


Anyway, these are largely niceties of style and of no real
significance in Python where you have no such choices, and you
can also define multiple return values (actually a tuple) and
so avoid even the scanf problem.

HTH,

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list