Showing posts with label python_scope. Show all posts
Showing posts with label python_scope. Show all posts

Mar 7, 2019

python change conf variables

conf.py
x=10

a.py
import conf
print 'Inside A: %d ' % (conf.x)
conf.x=20  #Here overwriting conf.x
print 'Inside A after changing value to 20 : %d ' % (conf.x)

b.py
import a
import conf
print 'Inside B: %d' % (conf.x)

Output:
python a.py
Inside A: 10
Inside A after changing value to 20 : 20

python b.py
Inside A: 10


Inside A after changing value to 20 : 20
Inside B: 20
#Here you get 20 as output, since you imported a 


When you import using 2 ways:
1) from conf import x 
#this will create local variable scope

2) import conf

conf.x  #refers to original location


a.py
from conf import x   #This creates variable in local scope
print 'Inside A: %d ' % (x)
x=20
print 'Inside A after changing value to 20 : %d ' % (x)

Output: python a.py
Inside A: 10
Inside A after changing value to 20 : 20

python b.py
Inside A: 10
Inside A after changing value to 20 : 20
Inside B: 10


Conclusion:
Always safer to use 
from conf import x
instead of 
import x





Jan 21, 2019

Python Local Vs Global

#########################
#   Global Scope Vs Enclosing Scope Vs Local Scope 
#   LEGB rule
#   Local(L): Defined inside function/class
#   Enclosed(E): Defined inside enclosing functions(Nested function concept)
#   Global(G): Defined at the uppermost level
#   Built-in(B): Reserved names in Python builtin modules
#########################

message = 'global'

def enclosing():
    message = 'enclosing'
    def local():
        message = 'local'
    print('enclosing message: ', message)   # enclosing
    local()
    print('enclosing message: ', message)   # enclosing


def enclosing_nonlocal():
    message = 'enclosing'
    def local():
        nonlocal message    # This refers to the above message in enclosing scope, not in global scope
        message = 'local'
    print('enclosing message: ', message)   # enclosing
    local()
    print('enclosing message: ', message)   local


def enclosing_global():
    message = 'enclosing'
    def local():
        global message
        message = 'local'  # Here you are updating message in global scope, not in enclosing scope
    print('enclosing message: ', message)   enclosing
    local()
    print('enclosing message: ', message)    enclosing 


if __name__ == '__main__':
    print('------------------------------------')
    print('global message: ', message)
    enclosing()
    print('global message: ', message)
    print('----------------NONLOCAL------------------')
    print('global message: ', message)
    enclosing_nonlocal()
    print('global message: ', message)
    print('----------------GLOBAL--------------------')
    print('global message: ', message)
    enclosing_global()
    print('global message: ', message)
    print('------------------------------------')

"""
Output:

------------------------------------
global message:  global
enclosing message:  enclosing
enclosing message:  enclosing
global message:  global
----------------NONLOCAL------------------
global message:  global
enclosing message:  enclosing
enclosing message:  local
global message:  global
----------------GLOBAL--------------------
global message:  global
enclosing message:  enclosing
enclosing message:  enclosing
global message:  local
------------------------------------

"""

Jun 24, 2018

Python Variables Scope

#!/usr/local/bin/python2.7

'''
    File name: python_scope.py
    Author: Prabhath Kota
    Date: June 22, 2018
    Python Version: 2.7
'''

'''
The scope of a variable refers to the places that you can see or access a variable.
If you define a variable at the top level of your script or module, this is a global variable
Module/global scope is a read-only shadow within the local function/method scope:
You can access it, providing there's nothing declared with the same name in local scope, but you can't change it.
'''

variable = 100
variable_new = 1111

def func_with_global_access():
    global variable
    print('variable inside function is : ', variable) #100
    variable = 200
    variable_new = 2222 #Local
    print('variable inside function - changed to : ', variable) #200
    print('variable_new inside function - changed to : ', variable_new) #2222

def func_without_global_access():
    try:
        print('variable_new inside fucntion - changed to : ', variable_new) #1111
        variable_new += 100
        """
        #Local variable 'variable_new' referenced before assignment
        #This is because module/global scope is a read-only shadow within the local function/method scope:
        #You can access it, providing there's nothing declared with the same name in local scope,
        #but you can't change it.
        """
        print('variable_new inside fucntion - changed to : ', variable_new) #2222
    except Exception, e:
        print e #local variable 'variable_new' referenced before assignment

print '----------------Global variable----------------------'
print ('variable outside is : ', variable) #100
func_with_global_access()
print ('variable outside is : ', variable) #200
print ('variable_new outside is : : ', variable_new) #1111

print '----------------Local variable----------------------'
print ('variable_new outside is : ', variable_new) #1111
func_without_global_access()
print ('variable_new outside is : ', variable_new) #1111