[Tutor] pointers for python?

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Oct 6 21:32:17 EDT 2003



On Sun, 5 Oct 2003, Thomi Richards wrote:

> > Can you say why? The return method is by far the best technique,
> > its safe and makes the function reusable - it doesn't rely on a
> > variable called x existing somewhere. Its "the right way" to do it.
>
> well, (for example), supposing you had a situation where a function was
> passed 3 variables ("A", "B", and "C"). A is an empty pygtk Table
> element, B is a list of strings.
>
> under C, you'd have to pass a pointer to A in order to modify it,
> correct? It just seemed neater doing someting like this:
>
> table = gtk.Table(3,2,gtk.FALSE)
> fill_table(table,["Label 1 text","label 2 text","Label 3 text"])
>
> than something like this:
> table = fill_table(gtk.table,["Label 1 text","label 2 text","Label 3 text"])


Hi Thomi,


Ok, good, a concrete example!  *grin*



Question: is gtk.table mutable?  If so, then good news: it's perfectly
possible to write the code you'd expect to write in Python.


I'm not familiar enough with PyGTK to decipher:

    http://www.gnome.org/~james/pygtk-docs/class-gtktable.html

yet --- I'm more familiar with Tkinter.  But if we pass a mutable object
to Python, we're perfectly set to do changes to it:


###
## Small demonstration of sending a function a mutable object
from Tkinter import *

def fill_frame_with_labels(frame, label_strings):
    for s in label_strings:
        Label(frame, text=s).pack()

if __name__ == '__main__':
    root = Tk()
    frame = Frame(root)
    fill_frame_with_labels(frame, ['label1', 'label2', 'label3'])
    frame.pack()
    mainloop()
###


Is this simliar to what you're looking for?


What we're talking about, then, is just a matter of "mutability" and
"immutability".  Maybe we should refocus the subject away from pointers
and to immutability concepts.



Talk to you later!




More information about the Tutor mailing list