All Questions
Tagged with scope python-3.x
267
questions
0
votes
1
answer
59
views
Positional arguments in nested functions
This question relates to this video: https://www.youtube.com/watch?v=jXugs4B3lwU
The piece I'm missing is somewhat simpler than the concept the video is covering overall. In the example code below:
...
0
votes
0
answers
65
views
Does mutability of type have an effect on scope of the variable?
I was playing around with global and local variables and noticed something different about lists and integers.
When I run the below code as expected two different local and global variables are ...
0
votes
0
answers
20
views
A question related to function enclosure and call-by-name of Python language [duplicate]
Consider the following code snippet:
def part1():
flist = []
for i in range(10):
def hello():
print(f"Hello {i = }.")
flist.append(hello)
for f ...
0
votes
2
answers
62
views
Some variables not being returned correctly from inside a function
I have declared a few functions in the main section of the program, which get used in one of my functions. guessList is being returned with the modified information correctly, but guess is not.
I am ...
0
votes
0
answers
58
views
Scope of Variables Within Nested Function [duplicate]
I find it rather confusing that the scope of variables within a nested function dependent on its type. Some variables must be declared as non-local variables while others do not. See the example below....
0
votes
1
answer
32
views
Order of Function Definitions and Referenced Before Assignmnet Error
Suppose we have the following two snippets of code:
Snippet #1:
def bar():
print("Hello world")
baz()
def baz():
print("Hello user")
bar()
Snippet #2:
def bar():
...
1
vote
1
answer
58
views
How do I access a variable defined outside my class in a separate file without running into circular imports?
I want to access my_variable defined in main.py within an instance of the SecondThread class. Here's my code
main.py
import threading
import time
import signal
import traceback
from second_thread ...
1
vote
0
answers
50
views
Python's multiprocessing scope behaving differently on two machines: why can't I access variables defined in '__main__'?
I am trying to understand why the scope on python's multiprocessing class is different between two computers. On the first computer, variables defined in '__main__' are not accessible in the function ...
0
votes
1
answer
465
views
Best way to implement --verbose flag globally in a python project
I have a python project with multiple functions. I've implemented verbose printing in one of the functions the following way
def func(a, b=None, verbose=False):
verboseprint = print if verbose ...
0
votes
1
answer
122
views
Python - Unexpected behaviour when accessing variable inside function that is defined outside function
This results in UnboundLocalError: cannot access local variable 'ans' where it is not associated with a value.
def a():
ans = 4
def traverse():
if ans == 2:
pass
...
0
votes
2
answers
43
views
scopes in python3: lists as parameters of a function?
I'm studying for a python3 exam and came across this question:
Given this snippet of code:
def my_function(my_list_1):
print("Print #1:", my_list_1)
print("Print #2:", ...
0
votes
2
answers
598
views
How to check that all variables inside a python function are local?
Given a Python function definition, is there some tool that can check that all variables used inside the function are local (either passed in as a parameter or declared within the function)?
0
votes
0
answers
17
views
Is there a way to nest a recursive function that updates a list with outer enclosing scope?
I'm trying to solve the following problem.
Given the root of a binary tree, construct a 0-indexed m x n string matrix res that represents a formatted layout of the tree. The formatted layout matrix ...
1
vote
0
answers
39
views
Is there a variable in python that represents the name of the local scope function it is being declared in? [duplicate]
To clarify, I am trying to log function calls in a database in the following manner
# Method to write to database a log of every function call along with the parameters
def write_program_log(...
1
vote
0
answers
39
views
Why does this code show the value of a global variable instead of a class variable when using a list comprehension? [duplicate]
So I've seen a code that goes like
name = "Python"
class Language:
name = "Java"
list1 = [name] * 3
list2 = [name for i in range(3)]
print(Language.list1)
print(...