Why are variables defined inside loop are accessible outside? [duplicate] - python

This question already has answers here:
What's the scope of a variable initialized in an if statement?
(7 answers)
Scoping in Python 'for' loops
(8 answers)
Closed 1 year ago.
for i in range(1, 10):
a =10
print(a)
It is giving me the output: 10
Expected output: Name is not defined
Can anyone please explain?

Related

print function that prints variable names and their values [duplicate]

This question already has answers here:
Python: Print a variable's name and value?
(13 answers)
Closed 2 years ago.
For debugging I often want to print a variable out. I do this with for example:
print("cat_litter", cat_litter)
That is I print the variable name and its value. Is it possible to define a function to do this for me so I could call something like "printwithname(cat_litter)" for example.
No function needed! Python 3.8+ f-strings support this kind of printing directly:
foo = 1
bar = 2
baz = "Something else"
print(f'{foo=}, {bar=}, {baz=}')
https://docs.python.org/3/whatsnew/3.8.html#f-strings-support-for-self-documenting-expressions-and-debugging
Python 3.6 -> 3.7 Workaround:
Python: Print a variable's name and value?

My code is not printing the executed code [duplicate]

This question already has answers here:
Python3 exec, why returns None?
(3 answers)
Closed 2 years ago.
print(exec("5 + 5"))
its not printing 10 but prints None instead. I'm sure exec basically executes a code.
exec returns None. You should probably use the eval() in this case

What is the Python class magic method for using is keyword? [duplicate]

This question already has answers here:
python: class override "is" behavior
(4 answers)
Is it possible to get a list of keywords in Python?
(7 answers)
Closed 4 years ago.
I know that in python we can use __eq__ method to check equality and __iter__ when using in keyword. Is there any method for is keyword?

How to "hint" the type of an empty list in python [duplicate]

This question already has answers here:
Type hinting a collection of a specified type
(5 answers)
Pycharm: Type hint list of items
(2 answers)
Closed 4 years ago.
I have a Class:
class MyClass(object):
def __init__(self):
self._subscribers = []
and i want to specify the type that will go into the list 'self._subscribers'. for example a queue.Queue() object. but the list should still be an empty list.
just for ease of use in PyCharm
self._subscribers = [queue.Queue]
self._subscribers = list[queue.Queue]
both don't seem to work
thanks
PS.: Python 3.64

is it possible to use a variable to name another variable? [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 5 years ago.
here is what I want to do:
b=1
har1=42
print(f"har{b}")
I want it to display 42
I have tried a few things, but I haven't managed to get it to work
Use either globals() or locals() and construct the key string.
b=1
har1=42
globals()['har'+str(b)]
def f():
b=1
har1=42
print(locals()['har'+str(b)])
f()

Categories

Resources