Local var available in global scope?

Ugur

In this code snippet I forgot to define the variable content in the outer scope. Then I assign a value to content in the with block. After leaving the with block the value of the variable content still exists. Why?

data_dir = "/Users/ugur/code/automating_tools/calibre"
kindle_drm_csv = "amazon_drm_books.csv"
csv_path = f"{data_dir}/{kindle_drm_csv}"


# I forgot to define this:
# content = ""
with open(csv_path, 'r', encoding='utf-8') as csv_file:
    # read whole content
    content = csv_file.read()

# why could I still print the content of the var?
# shouldn't it be undefined after leaving the `with` block?
print(content)

Also in this example:

if 3<10:
    local_var = True
print(local_var) 
# prints True
# why?
# I left the if-then-block. 
# Shouldn't the local_var be undefined after leaving the block?
Serge Ballesta

Python in not C. C automatic variables are indeed block scoped, but Python variables are either global or function scoped (*). As you are at the same function level, the variable declared in the block still exists after the end of the block.


(*) a corner case is that comprehensions are handled as hidden function. So a variable declared in a comprehension will hide any higher level scope variables of same name, and will vanish outside of the comprehension

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

local & global variable scope with 'var' keyword

Require in global scope or local scope?

Local/global scope

remove var from global scope

let vs var in the global scope

AngularJs $scope like a local var

Python scope between local and global

R: scope of local/global variables

Unable to bind local var to nested global var

Is there a simple method of turning a global var into a local var?

define variable of global scope in local scope

Python-Local Scope and Global Scope

LET vs VAR and Hoisting from Block Scope to Global Scope

Is using `var` to declare global scope as bad as using `global`

Creating Middleware Global vs local var

const variables in local and global scope in assembly

Should a class definition be put in the global or local scope?

python symbol can be global and local in the same scope

Static variables in global and local scope in C

JS local and global scope variable and function

Question about the local and global scope -JavaScipt

Convert local scope to global scope or query by parent attribute

Javascript - Global scope or local scope, not able to figure it out

Accessing variables in local scope and adding them to global scope in Julia

Variable is undefined in local scope if declared in upper scope and a var with same name is declared in local scope in an unexecuted statement

global var becomes local --UnboundLocalError: local variable referenced before assignment

JavaScript variable without var inside function not available in Global Execution Context

make variable from global scope available to class after import

Using 'var' and 'this' in the global scope in node.js program