Skip to content Skip to sidebar Skip to footer

Python: How Do I Call A Function Without Changing An Argument?

If I have a function: def foo(self, a, b): c = a + b return c How can I call foo without changing c in the function? So let's say I call foo in another function: def bar(

Solution 1:

Store c as class variable or global and override the function to return old value.

e.g.

classSomeClass:
     deffoo(self, a=None, b=None):
        if a and b:
            c = a + b
            self.stored_c = c
            return c
        return self.stored_c

Note: you will have to handle when to update stored_c and any concurrency issues.

Update: WRT glglgl's comment, updated for method overloading.

Solution 2:

c is local to the function and not static. That means that every time the function exits, c gets garbage collected. Why don't you just store the value of c as computed the first time? It seems like the obvious answer.

Solution 3:

I've taken what Rohan provided for an answer and come up with the following. It seems to work, although there may be a better/preferred way to accomplish this.

The following code allows me to keep track an account balance across multiple classes and methods.

import os

classFoo():
    def__init__(self):
        self.stored_end = 0defaccount(self, a, b):
        c = float(a) + b
        print a
        print b
        print c
        self.stored_end = c
        print self.stored_end

    deftesty(self, q, v):
        print"\n"print" _ " * 10
        z = float(q) + v
        print self.stored_end   
        self.stored_end = self.stored_end + z
        print" _ " * 10print self.stored_end

classBar():
    def__init__(self):
        passdefzippy(self, a, b):
        print" _ " * 10print"this is zippy"
        foo.testy(a, b)

classBaz():
    def__init__(self):
        passdefcracky(self, g, m):
        y = g + m
        print" _ " * 10print"calling stored_end"
        foo.stored_end = foo.stored_end + y
        print" _ " * 10print"this is cracky"print"y = %r" % y
        print foo.stored_end    

os.system("clear")      
foo = Foo()
foo.account(5, 11)
foo.testy(100, 100)
bar = Bar()
bar.zippy(10, 100)
baz = Baz()
baz.cracky(1000, 1)

Solution 4:

You'll need to have some construct to save the last result. E.g., you can do some wrapper to the function which does

defkeep_result(func):
    from functools import wraps
    @wraps(func)defwrapper(*a, **k):
        res = func(*a, **k)
        wrapper.last_result = res
        return res
    wrapper.func = func # makes it easy to bypassreturn wrapper

This is a so-called "decorator function".

Now if you do

@keep_resultdeffoo(self, a, b)
    c = a + b
    return c

the function foo (itself, not its result!) is used as an argument for keep_result() which creates a new function wrapper() which calls the original function, saves its result into an attribute and returns the result. This new function is returned in place of the original function foo().

So you can say

normal_result = foo(whatever)

and then do

saved_result = foo.last_result

and you get the same.

Solution 5:

why not store the result in self, and have optional arguments to see if it should to any calculations?

Something like:

deffoo(self, *args):
    if args:
        self.c = 0for value in args:
            self.c += value

    # In case `self.c` is not set yet, then use default of `0`returngetattr(self, 'c', 0)

Now if you call foo with arguments, it will add all arguments and store it. If called with no arguments it will return the last stored value.

Post a Comment for "Python: How Do I Call A Function Without Changing An Argument?"