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
>>>
8
  • 1
    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 Commented Aug 10, 2017 at 11:23
  • stackoverflow.com/questions/4693120/… Commented Aug 10, 2017 at 11:24
  • Or perhaps do you actually want to set default value for argument? In that case there is a neater way: def ex(filter,key="default key",user="default user"): Commented Aug 10, 2017 at 11:26
  • @v Sugumar I tried to do just ex(filter):, it tells me that user is not defined Commented Aug 10, 2017 at 11:31
  • please post the error and the script,, because it wont throw error Commented Aug 10, 2017 at 11:34

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

5 Comments

could be the problem that I am calling it wrongly from shell? please see my update.
Sorry for so many questions, but I am still new to python. what do you mean exactly by "global object in expens module"
sorry not global object it's global variable typing mistake
user should be a global variable in expens module
this one works but It is almost the same method that I am using in my function as mentioned in my question. my goal was to find some way not to rewrite user and key inside the function like this ex("2017/10",user,key)' after assigning their values. so just by writing ex("2017/10")`
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")

Comments

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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.