[Tutor] [python(x, y)] "Pointer" to a function? Storing a function as an object property? Passing arguments by value/by reference?

Alan Gauld alan.gauld at btinternet.com
Sat Jan 17 02:40:02 CET 2009


"Vicent" <vginer at gmail.com> wrote

> That "problem" has to contain, somehow, a property or element called
> "function" which, in fact, I would like it to be a function, or a 
> "pointer"
> to a function.

I won't repeat what others have said about passing function objects
into an object and storing them as attributes. However I will point 
out
an even more fundamental approach to your problem and pick up on
one of your comments...

> # call the function in prob, and store the result in "x" :
>
> x = prob.function( arguments/variables required by the function )

This is normal syntax for accessing the methods of an object.
Thus:

class Problem:
    def function(self, args):
          # your function code here
          return result

p = Problem()
x = p.function()

So if you know what function will be used for each problem
then you can define it as part of the class definition as a
normal method. Or you can pass a reference to a predefined
function into the class constructor:

class Problem:
     def __init__(self, aFunc):
         self.function = aFunc

def f(): return 42
def g(): return 66
p1 = Problem(f)
p2 = Problem(g)
x = p1.function()  # call f()
y = p2.function()  # call g()

This is IMHO slightly more elegant than assigning the function to
an attribute of the object after construction.

The down side of this style is that these functions will not
have any access to attributes inside your class via self, as methods
would.

> Does it makes any sense? Which would it be the right (meaning 
> efficient but
> still object-oriented-programming-compliant) way to do it?

Its all potentially object oriented, depending upon what you want to 
do.

> I mean, if I store "a whole function" within each "Problem" object 
> (assuming
> it can be done in Python), each Problem object would be consuming 
> lot of
> memory (wouldn't it?).

Methods are defined in classes (which are also objects in their own 
right
in Python) and objects hold references to the class. So each instance 
only
has a reference to the class which is used to access the predefined 
methods.
The functions that you pass as parameters as just direct references to 
those
function objects.

> Maybe it would be better just to store a kind of
> "pointer" to the function within the "problem" object,

Thats what happens.

> By the way, I have another related question. In C you can pass 
> arguments to
> a function by value or by reference. Is there any equivalence in 
> Python to
> that approach? How is the usual way to pass function arguments in 
> Python?

Basically by reference but as Wes said there are some wrinkles whoch 
mean
it can be less than intuitive for a beginner!

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list