local variable 'A' referenced before assignment code example

Example 1: python local variable referenced before assignment

variable = "Hello"
def function():
  global variable
  print(variable)
function()

Example 2: unboundlocalerror local variable referenced before assignment python

"""
This happens when python thinks that your variable is local
(the scope is only within a specific function) and/or that
you have not assigned a value to the variable before in this
specific function (scope).

try:
 - assigning the variable a value within the function
 - Passing the variable to the function using it when calling the function
 - Declaring the variable as global (bad practice)
 
Read more about this error in the source
"""

Example 3: local variable 'distro_name' referenced before assignment

// Solution for Python 3.8, procedure for other version might be a little different.

Edit the file: .../anaconda3/lib/python3.8/site-packages/anaconda_navigator/api/external_apps/vscode.py"

Add "DISTRO_NAME = None" at line 159 in the function _find_linux_install_dir.

154|    def _find_linux_install_dir(self):
155|        INST_DIR = None
156|        exe = os.path.join('/snap', 'bin', 'code')
157|        if os.path.lexists(exe):
158|            INST_DIR = '/snap'
159|        DISTRO_NAME = None
160|        for distro in self.distro_map.keys():
161|            _distro_regex = ".*{}/([^ ]*)".format(distro)
162|            m = re.match(_distro_regex, self._conda_api.user_agent)
163|            if m:
164|                DISTRO_NAME = distro
165|                DISTRO_VER = m.group(1)
166|                break

Tags:

Misc Example