del a => (1, 2, 3) print(a)# => NameError: name 'a' is not defined The first three statements show dynamic binding in action. The variable is bound to a string, then to a float, a then to another variable which in turn is assigned a tuple of value The statement then (1, 2, 3). del unbinds the variable from the tuple object which effectively deletes the variable (if there were no other a a references to the tuple object it too would have been deleted because an object with zero references is marked for garbage collection (but is not necessarily actually deleted immediately)). But in this case we see that is still b referencing the tuple object so the tuple is not deleted. The final statement above shows that an exception is raised when an unbound variable is referenced. The way in Python dynamically binds and rebinds variables is a source of some confusion to new programmers and even experienced programmers who are used to static binding where a variable is permanently bound to a single memory location. The Python language, by design, allows for dynamic binding and rebinding. Because Python performs a syntactic analysis and not a semantic analysis (with one exception which is covered in Applicability to language) and because of the dynamic way in which variables are brought into a program at run-time, Python cannot warn that a variable is referenced but never assigned a value. The following code illustrates this: if a > b: import x else: import y Depending on the current value of and either module or is imported into the program. If assigns a a b, x y x value to a variable and module references then, dependent on which import statement is executed first z y z (an import always executes all code in the module when it is first imported), an unassigned variable reference exception will or will not be raised. .2 Guidance to language users Avoid rebinding except where it adds value; Ensure that when examining code that you take into account that a variable can be bound (or rebound) to another object (of same or different type) at any time; and Variables to a function are deleted automatically when the encompassing function is exited but, though not a common practice, you can also explicitly delete variables using the statement when del they are no longer needed. Unused Variable [YZS] The applicability to language and guidance to language users sections of the write-up are applicable here.
del a => (1, 2, 3) print(a)# => NameError: name 'a' is not defined The first three statements show dynamic binding in action. The variable a is bound to a string, then to a float, then to another variable which in turn is assigned a tuple of value (1, 2, 3). The del statement then unbinds the variable a from the tuple object which effectively deletes the a variable (if there were no other references to the tuple object it too would have been deleted because an object with zero references is marked for garbage collection (but is not necessarily actually deleted immediately)). But in this case we see that b is still referencing the tuple object so the tuple is not deleted. The final statement above shows that an exception is raised when an unbound variable is referenced. The way in Python dynamically binds and rebinds variables is a source of some confusion to new programmers and even experienced programmers who are used to static binding where a variable is permanently bound to a single memory location. The Python language, by design, allows for dynamic binding and rebinding. Because Python performs a syntactic analysis and not a semantic analysis (with one exception which is covered in Applicability to language) and because of the dynamic way in which variables are brought into a program at run-time, Python cannot warn that a variable is referenced but never assigned a value. The following code illustrates this: if a > b: import x else: import y Depending on the current value of a and b, either module x or y is imported into the program. If x assigns a value to a variable z and module y references z then, dependent on which import statement is executed first (an import always executes all code in the module when it is first imported), an unassigned variable reference exception will or will not be raised. .2 Guidance to language users Avoid rebinding except where it adds value; Ensure that when examining code that you take into account that a variable can be bound (or rebound) to another object (of same or different type) at any time; and Variables to a function are deleted automatically when the encompassing function is exited but, though not a common practice, you can also explicitly delete variables using the del statement when they are no longer needed. Unused Variable [YZS] The applicability to language and guidance to language users sections of the write-up are applicable here.
ISO-IECJTC1-SC22-WG23_N0541-tr24772-4-draft-2015-05-26.docx