1

I have the following function:

def ex(filter, key, user):
    login = user
    secret = key
    f1 = filter 
    "..." #many other comands

user = "some text"
key = "some numbers"
filter = "2017/10"

to apply this function I write:

user = "some text"
key = "some numbers"
ex("2010/10",key,user)

This function works without any problems. My question is how can I make the local variables taking values from the global ones? so I want to be able to apply my code just by writing the following:

user = "some text"
key = "some numbers"
ex("2017/10")

Any suggestions?

Update

After wririting the code according to @v Sugumar. Calling the function from shell

>>> import expens as e
>>> user = "XXX"
>>> key = "111"
>>> e.exp("08/2017")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\ad\Anaconda4\lib\expens.py", line 29, in exp
    userid = user
NameError: name 'user' is not defined
>>>
Kingindanord
  • 1,754
  • 2
  • 19
  • 48

3 Answers3

2

There are couple of ways to do it:

  1. Drop the function arguments and they will be automatically picked from global scope. Read: Short Description of the Scoping Rules?

user = "some text"
key = "some numbers"

def ex(filter):
    login = user
    secret = key
  1. Give them static default values or pick the default values using the global variables.

user = "some text"
key = "some numbers"

def ex(filter, key="some numbers", user="some text"):  # static defaults
def ex(filter, key=key, user=user):  # defaults picked from global variables
  1. Don't modify the function at all and use a partial function with user and key applied to it.

from functools import partial

ex_with_user_and_key = partial(ex, key="some numbers", user="some text")
ex_with_user_and_key("2017/10")
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
2

just create function def ex(filter): before that define user and key and use the variables in the function.. if a variable is not found inside the function then it will be searched the inthe enclosing scope and then in the global scope and then in the builtins and if you don't want to change the global variables inside the function then you don't need to use global before the variable names before using them

enter image description here call like this

 #file listdir.py

 def ex(filter):
    login = user
    secret = key
    f1 = filter 
    print(login)
    print(secret)

user = "some text"
key = "some numbers"
ex("2017/10")

update

 #file listdir.py
def ex(filter,user,key):
    login = user
    secret = key
    f1 = filter 
    print(login)
    print(secret)

enter image description here

update 2

ok then define the variables

 #file listdir.py
def ex(filter):
    login = user
    secret = key
    f1 = filter 
    print(login)
    print(secret)

enter image description here

Sugumar Venkatesan
  • 4,019
  • 8
  • 46
  • 77
0

Define the function as def ex(filter):

The function will automatically pickup the global variables if not found in the local scope (thanks to Ashwini to pointing out you don't need to use global). So you can define your function as:

def ex(filter):
    login = user
    secret = key
    f1 = filter 
    "..." #many other comands

and then when calling it, just pass in filter and user and key will be picked up from the global scope. So it is fine to call:

ex("2017/10")
Joe Iddon
  • 20,101
  • 7
  • 33
  • 54