Free
20 Questions 20 Marks 18 Mins
Latest UPPCL Assistant Accountant Updates
Last updated on Oct 25, 2022
The Uttar Pradesh Power Corporation Limited [UPPCL] has released a new notification for the recruitment process of UPPCL Assistant Accountant 2022. A total of 186 vacancies have been released. Candidates can apply online from 8th November and the last date to apply is 28th November 2022. Candidates who want a successful selection for the post must refer to the UPPCL Assistant Accountant Preparation Tips to improve their preparation for the UPPCL exam.
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...
asked Jan 7, 2009 at 6:21
2
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
.
tzot
89.6k29 gold badges138 silver badges201 bronze badges
answered Jan 7, 2009 at 11:13
S.LottS.Lott
377k79 gold badges503 silver badges773 bronze badges
5
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
answered Jan 7, 2009 at 6:25
too much phptoo much php
86.7k34 gold badges128 silver badges135 bronze badges
3
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.
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
answered Jan 7, 2009 at 6:26
J.J.J.J.
4,8181 gold badge24 silver badges29 bronze badges
2
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.
answered Jan 7, 2009 at 13:59
Steve LoshSteve Losh
19.5k2 gold badges51 silver badges44 bronze badges
3
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.
answered Jan 7, 2009 at 21:35
NickNick
20.8k18 gold badges47 silver badges50 bronze badges
1
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.
answered Jan 8, 2009 at 0:26
Alex RAlex R
2,1931 gold badge18 silver badges32 bronze badges