16 a = s+i
17 if a == a[::-1]:
18 a1.append(i)
19 for i in a1:20 if i == i[::-1]:
21 a2.append(i)
22 a2.sort(key = len)
12 for i in range(len(s)+1):
13 for j in range(i+1,len(s)+1):
14 a.append(s[i:j])
15 for i in a:16 a = s+i
17 if a == a[::-1]:
18 a1.append(i)
9 a = []
10 a1= []
11 a2 = []
12 for i in range(len(s)+1):13 for j in range(i+1,len(s)+1):
14 a.append(s[i:j])
15 for i in a:
1from itertools import product
2def dfs(dp):
3 m, n, ans = len(dp), len(dp[0]), 0 4 for i, j in product(range(m-1, -1, -1), range(n-1,-1, -1)):
5 if i == m-1 or j == n-1:
6 dp[i][j] = int(dp[i][j])
1from itertools import product
2def dfs(dp):
3 m, n, ans = len(dp), len(dp[0]), 0 4 for i, j in product(range(m-1, -1, -1), range(n-1,-1, -1)):
5 if i == m-1 or j == n-1:
6 dp[i][j] = int(dp[i][j])
The local variable name hides the variable defined in the outer scope, making it inaccessible and might confuse.
filename = 'myfile.txt'
def read_file(filename): # This shadows the global `filename`
with open(filename) as file:
return file.readlines()
FILENAME = 'myfile.txt' # renamed global to UPPER_CASE as convention
def read_file(filename):
with open(filename) as file:
return file.readlines()
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)
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()