4

Hi I am getting confused of how python is able to access a variable declared-instantiated inside try block from out of it. How is this working at memory level?

Here is a block of code in which I am getting confused. I am on Python 3.8

def demo_method():
    try:
        num = float(input("Please enter a positive number (floating point) :"))
    except ValueError:
        num = float(input("Please enter a positive number (floating point) :"))

    print("Value Entered ", num) ##Doubt: Why is this num tagged to the value fetched in either try block or except block


demo_method()

In the above code snippet, num is declared inside try(local to itself) and also except(local to itself). How can print statement which is outside of both try and except able to access this variable.

I am from a java background and I new to python. I would really appreciate an elaborate answer. Thank You

R96R
  • 99
  • 9
  • 3
    Python does not have variable declarations or block scope. – user2357112 Feb 24 '20 at 06:27
  • Thanks for the speedy response :) So, this would mean for variable accessibility python only check whether a variable is instantiated inside a method or not? If a variable is inside a method it would be accessible throughout the function irrespective of whether it is inside try-except block or not. – R96R Feb 24 '20 at 06:35
  • 2
    Caution though. If an exception did occur, then `num` may not be defined when you get ejected from that block of code, unless it was set before the statement that raised an error. And if you put variable assignments in blocks of code that conditionally may not execute every time.. your code could eventually throw a NameError if you try to read a variable that was skipped. So follow good practices and don't count on variables inside conditional blocks to be set outside them @R96R – Todd Feb 24 '20 at 06:44
  • 2
    In this specific example, num is assigned on each path through the code, so it's safe to access it.. well.. maybe not.. `input()` could fail twice. – Todd Feb 24 '20 at 06:46
  • 2
    When you first load a module, python compiles its functions and methods. Each of these get a function object that has a slot for each of the variables assigned in the function (unless that variable is declared `global`). So, `num` becomes, say, slot 1. If you try to use slot 1 before its assigned an object, you get an UnboundLocalError. After its assigned, anything in the function can use it. If you `del num`, the slot is unbound and you go back to the use-before-assignment case. – tdelaney Feb 24 '20 at 06:52
  • @Todd, ...if it _did_ fail a second time, there'd be an unhandled exception so you wouldn't reach the access. – Charles Duffy Aug 11 '23 at 17:04

1 Answers1

0

I think this is more of a "block scope" question (vs specific to a try/catch blocks). Here's an answer I found: Block scope in Python

Coming from a Java/C++ background, this is something that takes getting used to :-)

shubh
  • 21
  • 2
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/34830632) – geanakuch Aug 17 '23 at 12:27