pointer in Python

Gerhard Häring gerhard.haering at opus-gmbh.net
Wed Mar 5 13:19:39 EST 2003


Roy Marteen <rm at no-mail.com> wrote:
> Hi,
> 
> Can I parse a variable using a pointer in Python?
> And how can I declare pointer to function in Python? Thank you very much.

Python doesn't have pointers, it has names and objects. Everything is an
object, including numbers, strings, types, classes, modules ... and
functions.

Here's a contrived example [1] using a function reference:

#v+
def square(x):
    return x * x

def apply_function_to_list(func, lst):
    temp_list = []
    for element in lst:
        temp_list.append(func(element))
    return temp_list

print apply_function_to_list(square, [3,4,5])
#v-

Not sure what you mean with "parsing a variable", but if it's a string,
surely the methods of the string will come in helpful:

http://www.python.org/doc/current/lib/string-methods.html

-- Gerhard

[1] This can actually be written quite shortly using list comprehensions:
print [x*x for x in [3,4,5]]




More information about the Python-list mailing list