Redefining name 'data' from outer scope (line 11)
20
21
22def generate_table(_dataframe, _max_rows=10):
23 data = pd.read_excel(24 "data/2018/aggregates-economic-activity/S7.1.xlsx", header=None
25 )
26 df = data[3:]
Redefining name 'rows' from outer scope (line 18)
79layout = app_layout()
80
81
82def filter(year, _category, rows, labels, remove=False): 83 cu_index, co_index = [
84 index for index in years.transpose().index if years[index].iloc[0] == year
85 ]
Redefining name 'labels' from outer scope (line 19)
79layout = app_layout()
80
81
82def filter(year, _category, rows, labels, remove=False): 83 cu_index, co_index = [
84 index for index in years.transpose().index if years[index].iloc[0] == year
85 ]
Redefining name 'labels' from outer scope (line 22)
83layout = app_layout()
84
85
86def filter(year, _category, rows, labels, remove=False): 87 cu_index, co_index = [
88 index for index in years.transpose().index if years[index].iloc[0] == year
89 ]
Redefining name 'rows' from outer scope (line 21)
83layout = app_layout()
84
85
86def filter(year, _category, rows, labels, remove=False): 87 cu_index, co_index = [
88 index for index in years.transpose().index if years[index].iloc[0] == year
89 ]
Redefining name 'rows' from outer scope (line 24)
86layout = app_layout()
87
88
89def filter(year, _category, rows, labels, remove=False): 90 cu_index, co_index = [
91 index for index in years.transpose().index if years[index].iloc[0] == year
92 ]
Redefining name 'labels' from outer scope (line 25)
86layout = app_layout()
87
88
89def filter(year, _category, rows, labels, remove=False): 90 cu_index, co_index = [
91 index for index in years.transpose().index if years[index].iloc[0] == year
92 ]
Redefining name 'rows' from outer scope (line 17)
79layout = app_layout()
80
81
82def filter(year, _category, rows, labels, remove=False): 83 cu_index, co_index = [
84 index for index in years.transpose().index if years[index].iloc[0] == year
85 ]
Redefining name 'labels' from outer scope (line 18)
79layout = app_layout()
80
81
82def filter(year, _category, rows, labels, remove=False): 83 cu_index, co_index = [
84 index for index in years.transpose().index if years[index].iloc[0] == year
85 ]
Redefining name 'data' from outer scope (line 18)
61
62
63def generate_table(_dataframe, _max_rows=10):
64 data = pd.read_excel(filename, header=None) 65 df = data[6:-1]
66 df.columns = df.iloc[0].fillna(value=pd.Series(range(100)))
67 return dash_table.DataTable(
Redefining name 'data' from outer scope (line 19)
65
66
67def generate_table(_dataframe, _max_rows=10):
68 data = pd.read_excel( 69 "data/2018/aggregates-economic-activity/S7.1.xlsx", header=None
70 )
71 df = data[3:]
Description
The local variable name hides the variable defined in the outer scope, making it inaccessible and might confuse.
Bad practice
filename = 'myfile.txt'
def read_file(filename): # This shadows the global `filename`
with open(filename) as file:
return file.readlines()
Preferred:
FILENAME = 'myfile.txt' # renamed global to UPPER_CASE as convention
def read_file(filename):
with open(filename) as file:
return file.readlines()
Bad practice
Another usual suspect of this is when you use the same parameter name inside a function as the global variable you are using. For example:
def run_app(app):
# This `app` shadows the global app...
app.run()
if __name__ == '__main__':
app = MyApp() # This is a global variable!
run_app(app)
Preferred:
To avoid this re-defining of a global, consider not defining app
as a global, but inside a main()
function instead:
def run_app(app):
# There is no longer a global `app` variable.
app.run()
def main():
app = MyApp()
run_app(app)
if __name__ == '__main__':
main()