Skip to main content

All Questions

0 votes
0 answers
21 views

Python function variable scoping and expressions [duplicate]

I notice that global variables in Python (3.9.10) can be accessed within functions, but not mutated as shown below: No error: def absolute_value(number): print(y) if number < 0: ...
baubel's user avatar
  • 97
0 votes
1 answer
947 views

Python - Get name list of objects of a specified class in the namespace

I want to get a list of all dictionary objects that exist in the namespace. I know that %whos() magic function will print the list, but as far as I could tell, it just prints and doesn't return ...
rahul-ahuja's user avatar
  • 1,424
-1 votes
2 answers
285 views

How to stop variables becoming local?

I'm trying to create a function to verify if a move in a game is legal. At the top of my code, I declared the variable Valid. p1 = [1,1] p2 = [1,1] turn = 1 move = 0 Valid = False and later on, I ...
Jaxon y's user avatar
  • 17
0 votes
2 answers
505 views

Python local variable 'message' referenced before assignment problem

liquid = [l_press,l_dosage,l_bottle,l_position,l_clog,l_counter] for j in range(len(liquid)): data = liquid_filling(liquid[j]) publish.single(liquid[j],data,qos = 0, hostname = ...
cbcliff47's user avatar
-1 votes
1 answer
74 views

Assigning variables in a function

In the code below, I can see that the variables end_line and internal_line are assigned to their characters but then below that in the for loop, another variable called end_line is being assigned to ...
Britt's user avatar
  • 11
2 votes
0 answers
38 views

Python: A global variable in a local function possible? [duplicate]

Consider the programm: x = 0 def f(): c = 2 def g(): c = 4 print(c) print(c) I want that the c in g() is the same as the c in f(). Normally one would use the keyword ...
ZPlaya7's user avatar
  • 31
-1 votes
1 answer
40 views

User enter a date string ('year' or 'month') and it applies to date.userinputdate

I have term = input("Enter a variable name") #choose the term (i.e.,day,week,month) and in my code there is a section as below date.term I like users can input 'month' or 'year' and the ...
Samtry's user avatar
  • 35
0 votes
1 answer
56 views

Is this a good practice with functions? [closed]

I am working on my very first python programming. I have no PC programming experience. I was having issues with using global variables inside my function. So I defined them again. Here is the top of ...
Kevin Lay's user avatar
1 vote
1 answer
133 views

Is using globals() or locals() to avoid the creation of a rarely needed variable a good idea?

I have a script that processes folders. A few folders (say 5% of them) have a special status assigned to them based on their content. I use a variable special = True to store this status, which later ...
aed's user avatar
  • 80
0 votes
1 answer
1k views

Setting **kwargs as local variables

I wanted to receive some arguments via **kwargs and convert them to local variables. So I first tried like this: for k, v in kwargs.items(): cmd = k + ' = ' + stringify(v) exec(cmd) Which ... ...
fkarg's user avatar
  • 198
0 votes
1 answer
212 views

Python program that identifies variables of a .py file and prints them

I need to create a program that finds all the variables of .py file(globals and locals) and print them. But I don't know how to use the function dir() or global()/local() on the other .py file for ...
Brush's user avatar
  • 31
0 votes
2 answers
999 views

How to acces items in dictionary as variables inside another function?

I'm writing a script that has to intitializate and read lots of variables and some files before actually calculating some stuff. Since I need to perform the calculations several times I separated the ...
jaarpa's user avatar
  • 35
1 vote
1 answer
216 views

Maximise the f_locals dictionary in spyder variable explorer

This is a Spyder IDE specific question Spyder IDE does not show local variables of a method in variable explorer, it only shows the global variables. So to get the local variables in variable ...
Jis Mathew's user avatar
0 votes
1 answer
57 views

python Fetch existing variables into a list [duplicate]

I've used locals() function and created some variables. Now I need to fetch all the variables created into a list, how should I do? example: column_list=[x for x in range(100)] for i,j in enumerate(...
S.Gu's user avatar
  • 649

15 30 50 per page