{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "###
San Jose State University
Dep|artment of Applied Data Science

**DATA 200
Computational Programming for Data Analytics**

Spring 2024
Instructor: Ron Mak
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 4.13 Scope Rules" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Local Scope" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### A variable defined inside a function has **local scope**. You can use it only inside that function. It's accessible from where you first define it to the end of the function. Any variable with the same name defined outside the function or in another function is a **different** variable." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def scope_test():\n", " n = 12\n", " print(f' Inside scope_test: {n = }')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n = 20\n", "print(f'Outside scope_test: {n = }')\n", "\n", "scope_test()\n", "print(f'Outside scope_test: {n = }')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Global Scope" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### A variable defined outside any function has **global scope**. You can access a global variable inside a function, as long as the function doesn't have a local variable with the same name." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Accessing a Global Variable from a Function " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = 7" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def access_global():\n", " print('x printed from access_global:', x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "access_global()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### However, you cannot change the value of a global variable from within a function. In the following function, the variable `x` is a local variable that is different from the global variable `x`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def try_to_modify_global():\n", " x = 3.5\n", " print('x printed from try_to_modify_global:', x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "try_to_modify_global()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The global variable `x` was not changed." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### But if you do want to change a global variable from within a function, you must declare the global variable." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def modify_global():\n", " global x;\n", " x = 'hello'\n", " print('x printed from modify_global:', x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "modify_global()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Now the global variable has changed." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Using global variables can lead to confusing, error-prone programs. You should avoid them as much as possible." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Shadowing Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Be careful that you don't give a variable the same name as a built-in function." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sum([10, 5])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sum = 10 + 5" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sum" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sum([10, 5])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "##########################################################################\n", "# (C) Copyright 2019 by Deitel & Associates, Inc. and #\n", "# Pearson Education, Inc. All Rights Reserved. #\n", "# #\n", "# DISCLAIMER: The authors and publisher of this book have used their #\n", "# best efforts in preparing the book. These efforts include the #\n", "# development, research, and testing of the theories and programs #\n", "# to determine their effectiveness. The authors and publisher make #\n", "# no warranty of any kind, expressed or implied, with regard to these #\n", "# programs or to the documentation contained in these books. The authors #\n", "# and publisher shall not be liable in any event for incidental or #\n", "# consequential damages in connection with, or arising out of, the #\n", "# furnishing, performance, or use of these programs. #\n", "##########################################################################\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Additional material (C) Copyright 2024 by Ronald Mak" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" } }, "nbformat": 4, "nbformat_minor": 4 }