I have the following code:
def all_dsc(x):
dict={'a':x+2,'b':x-2}
return dict
I would like to to call keys from specifically this dictionary outside of the function. smthn like:
all_dsc.dict.keys()
What's the best way of doing it?
I have the following code:
def all_dsc(x):
dict={'a':x+2,'b':x-2}
return dict
I would like to to call keys from specifically this dictionary outside of the function. smthn like:
all_dsc.dict.keys()
What's the best way of doing it?
You can't access locals in a function from outside the function. All you can do is return the dictionary object by calling the function, which you already do.
Call the function, and then just treat the result of the call expression as a dictionary:
all_dsc(42).keys()
Demo:
>>> all_dsc(42)
{'a': 44, 'b': 40}
>>> all_dsc(42).keys()
['a', 'b']
Note that you really shouldn't use the name of a built-in type as a variable if you can avoid it. dict
is a built-in type, so try to use a different name here:
def all_dsc(x):
mapping = {'a': x + 2, 'b': x - 2}
return mapping
or even avoid creating a local at all, since it doesn't matter here:
def all_dsc(x):
return {'a': x + 2, 'b': x - 2}