Scope<\/strong><\/p>\nA scope defines the visibility of a name within a block. If a local variable is defined in a block, its scope includes that block. If the definition occurs in a function block, the scope extends to any blocks contained within the defining one. The scope of names defined in a class block is limited to the class block. If a name is bound in a block, it is a local variable of that block. If a name is bound at the module level, it is a global variable. The variables of the module code block are local and global.<\/p>\n
In Python, variables that are only referenced inside a function are implicitly global. If a variable is ever assigned a new value inside the function, the variable is implicitly local, and the programmer need to explicitly declare it as global.<\/p>\n
The scope is bit difficult to understand, the following examples might prove fruitful.<\/p>\n
def f ( ) :\r\n\u00a0 \u00a0 \u00a0 \u00a0 print s\r\ns=\" I hate spam \"\r\nf ( )<\/pre>\nThe variable s is defined as the string “I hate spam”, before the function call f ( ). The only statement in f ( ) is the print statement. As there is no local variable s in f ( ), the value from the global s will be used. So the output will be the string “I hate spam”. The question is, what will happen, if the programmer changes the value of s inside of the function f ( ) ? Will it affect the global s as well? The test is in the following piece of code:<\/p>\n
def f ( ) :\r\n\u00a0 \u00a0 \u00a0 s=\"Me too.\" \r\n\u00a0 \u00a0 \u00a0 print s\r\ns=\" I hate spam.\" \r\nf ( )\r\nprint s<\/pre>\nThe output looks like the following. It can be observed that s in f ( ) is local variable of f ( ).<\/p>\n
Me too.\r\nI hate spam.<\/pre>\nThe following example tries to combine the previous two examples i.e. first access s and then assigning a value tp it in function
\nf ( ).<\/p>\n
def f ( ) :\r\n\u00a0 \u00a0 print s \r\n\u00a0 \u00a0 s=\"Me too.\" \r\n\u00a0 \u00a0 print s\r\n\r\ns=\" I hate spam.\" \r\nf ( )\r\nprint s<\/pre>\nThe code will raise an exception- UnboundLocalError: local variable ‘s’ referenced before assignment<\/p>\n
Python assumes that a local variable is required due to the assignment to s anywhere inside f ( ), so the first print statement gives the error message. Any variable which is changed or created inside of a function is local, if it has not been declared as a global variable. To tell Python to recognize the variable as global, use the keyword global, as shown in the following example.<\/p>\n
def f ( ) :\r\n\u00a0 \u00a0 \u00a0 global s \r\n\u00a0 \u00a0 \u00a0 print s\r\n\u00a0 \u00a0 \u00a0 s=\" That's clear.\" \r\n\u00a0 \u00a0 \u00a0 print s\r\n\r\ns=\"Python is great ! \" \r\nf ( )\r\nprint s\r\n\r\n<\/pre>\nNow there is no ambiguity. The output is as follows:<\/p>\n
Python is great !\r\nThat's clear.\r\nThat's clear.<\/pre>\nLocal variables of functions cannot be accessed from outside the function code block.<\/p>\n
def f ( ) :\r\ns=\" I am globally not known\" \r\n\u00a0 \u00a0 \u00a0 \u00a0print s\r\nf ( )\r\nprint s<\/pre>\nExecuting the above code will give following error message- NameError : name ‘s’ is not defined<\/p>\n","protected":false},"excerpt":{"rendered":"
In this Page, We are Providing Python Programming \u2013 Scope. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. Python Programming \u2013 Scope Scope A scope defines the visibility of a name within a block. If a local variable is defined in a block, its scope includes that block. If the definition …<\/p>\n
Python Programming \u2013 Scope<\/span> Read More »<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython Programming \u2013 Scope - Python Programs<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n