views:

489

answers:

7

Its urgent...

I have a function. Inside that I'm maintainfing a dictionary of values. I want that dictionary to be maintained between different function calls

Suppose the dic is :

a = {'a':1,'b':2,'c':3}

At first call,say,I changed a[a] to 100 Dict becomes a = {'a':100,'b':2,'c':3}

At another call,i changed a[b] to 200 I want that dic to be a = {'a':100,'b':200,'c':3}

But in my code a[a] doesn't remain 100.It changes to initial value 1.

I need an answer ASAP....I m already late...Please help me friends...

+10  A: 

You could use a static variable:

def foo(k, v):
  foo.a[k] = v
foo.a = {'a': 1, 'b': 2, 'c': 3}

foo('a', 100)
foo('b', 200)

print foo.a
too much php
This works, but it's not common practice.
ΤΖΩΤΖΙΟΥ
-1 for using function to do class work.
nosklo
@ΤΖΩΤΖΙΟΥ neither are stateful callables. I don't see much benefit in using a class to create a stateful function other than separating initialization from use.
Aaron Maenpaa
The reason this is "class work" is that you might want two functionally identical functions, but with different data.
gnud
-1: static variables in classes or in functions.
S.Lott
@Peter: a class instance is a binding of data and code. Exactly what the OQ asked for. You obviously have the right to support your suggested answer, but voting down other solutions that *are* helpful shows inflexibility to say the least.
ΤΖΩΤΖΙΟΥ
+6  A: 

If 'a' is being created inside the function. It is going out of scope. Simply create it outside the function(and before the function is called). By doing this the list/hash will not be deleted after the program leaves the function.

a = {'a':1,'b':2,'c':3}

# call you funciton here
J.J.
I tried this several different ways, all of which seem to work :) And best of all, it's very simple!
Miquella
-1 for using global variables which can lead to confusion
nosklo
@nosklo: He asked for a solution. Without the code we don't know that our list is being generated globally. He could have all this already wrapped inside another function, or class.
J.J.
+13  A: 

You might be talking about a callable object.

class MyFunction( object ):
    def __init__( self ):
        self.rememberThis= dict()
    def __call__( self, arg1, arg2 ):
        # do something
        rememberThis['a'] = arg1
        return someValue

myFunction= MyFunction()

From then on, use myFunction as a simple function. You can access the rememberThis dictionary using myFunction.rememberThis.

S.Lott
I edited your answer because MyFunction() wouldn't call the function, it would create an instance with no visible side-effects and it would be confusing.
ΤΖΩΤΖΙΟΥ
+1 for using a class to do a class job
nosklo
-1 for using a class and and magic functions to do something trivial
too much php
I'm on trying to be aggressive, but your implementation just seems far more complicated than it needs to be ...
too much php
By "magic functions", do you mean special method names?(http://www.python.org/doc/2.5.2/ref/specialnames.html) Or are you referring to something else?
S.Lott
Yes I'm talking about __call__() and the use of a custom object to replace the standard 'function' type.
too much php
+3  A: 

You can 'cheat' using Python's behavior for default arguments. Default arguments are only evaluated once; they get reused for every call of the function.

>>> def testFunction(persistent_dict={'a': 0}):
...     persistent_dict['a'] += 1
...     print persistent_dict['a']
...
>>> testFunction()
1
>>> testFunction()
2

This isn't the most elegant solution; if someone calls the function and passes in a parameter it will override the default, which probably isn't what you want.

If you just want a quick and dirty way to get the results, that will work. If you're doing something more complicated it might be better to factor it out into a class like S. Lott mentioned.

EDIT: Renamed the dictionary so it wouldn't hide the builtin dict as per the comment below.

Steve Losh
+1, but I would use a different name than dict, as you're overwriting the builtin function within that scope.
recursive
Good point -- edited.
Steve Losh
+1, you stole my answer :)
Triptych
This works (and it's very useful to know about, too) but the Paranoid Programmer inside me worries that it's too easy to replace the dictionary by providing an extra argument.
too much php
That kind of practice is just asking for obscure bugs to sneak into your program.
Algorias
@Peter - that can be a feature. you have one interface for modifying state as well as resetting it. depends on the goals of the code...
Daniel
+3  A: 

Rather than forcing globals on the code base (that can be the decision of the caller) I prefer the idea of keeping the state related to an instance of the function. A class is good for this but doesn't communicate well what you are trying to accomplish and can be a bit verbose. Taking advantage of closures is, in my opinion, a lot cleaner.

def function_the_world_sees():
    a = {'a':1,'b':2,'c':3}

    def actual_function(arg0, arg1):
        a[arg0] = arg1
        return a

    return actual_function
stateful_function = function_the_world_sees()

stateful_function("b", 100)    
stateful_function("b", 200)

The main caution to keep in mind is that when you make assignments in "actual_function", they occur within "actual_function". This means you can't reassign a to a different variable. The work arounds I use are to put all of my variables I plan to reassign into either into a single element list per variable or a dictionary.

Ed
+1  A: 

This question doesn't have an elegant answer, in my opinion. The options are callable objects, default values, and attribute hacks. Callable objects are the right answer, but they bring in a lot of structure for what would be a single "static" declaration in another language. Default values are a minor change to the code, but it's kludgy and can be confusing to a new python programmer looking at your code. I don't like them because their existence isn't hidden from anyone who might be looking at your API.

I generally go with an attribute hack. My preferred method is:

def myfunct():
    if not hasattr(myfunct, 'state'): myfunct.state = list()
    # access myfunct.state in the body however you want

This keeps the declaration of the state in the first line of the function where it belongs, as well as keeping myfunct as a function. The downside is you do the attribute check every time you call the function. This is almost certainly not going to be a bottleneck in most code.

Nick
A: 

Personally, I like the idea of the global statement. It doesn't introduce a global variable but states that a local identifier actually refers to one in the global namespace.

d = dict()
l = list()
def foo(bar, baz):
    global d
    global l
    l.append(bar, baz)
    d[bar] = baz

In python 3.0 there is also a "nonlocal" statement.

Alex R