Redefining name 'app' from outer scope (line 1)
4import requests
5
6@app.on_message(filters.command("stack", prefix) & filters.me)
7async def stack(app, msg): 8 await msg.edit("Processing...")
9 if len(msg.command) < 2:
10 await msg.edit(f"`{prefix}stack` **query**")
Redefining name 'app' from outer scope (line 1)
4import requests
5
6@app.on_message(filters.command("repo", prefix) & filters.me)
7async def search_repo(app, msg): 8 url = "https://api.github.com/search/repositories?q="
9 if len(msg.command) < 2:
10 await msg.edit(f"`{prefix}search-repo` **query**")
Redefining name 'app' from outer scope (line 2)
33
34# create query command for neko2
35@app.on_message(filters.command("neko2", prefix) & filters.me)
36async def neko2(app, msg):37 url = "https://nekos.life/api/v2/img/"
38 if len(msg.command) < 2:
39 await msg.edit(f"`{prefix}neko2` **category**")
Redefining name 'app' from outer scope (line 2)
55
56# list of categories from data
57@app.on_message(filters.command("neko2list", prefix) & filters.me)
58async def neko2list(app, msg):59 # parse data to string and make list of categories
60 categories = "\n-".join(data)
61 # usage of categories
Redefining name 'data' from outer scope (line 7)
43 if r.status_code == 404:
44 await msg.edit("Category not found")
45 return
46 data = r.json()47 url = data["url"]
48 await msg.edit(f"**Neko for {category}**")
49 # fix gif image
Redefining name 'app' from outer scope (line 1)
76
77# list of categories from data dsfw and dnsfw for nekolist
78@app.on_message(filters.command("nekolist", prefix) & filters.me)
79async def getNekoList(app, msg):80 sfw = "\n- ".join(dsfw)
81 nsfw = "\n- ".join(dnsfw)
82 await msg.edit(f"**SFW (Safe for work) list** \n\n- {sfw}\n\n**NSFW (NOT safe for work) list** \n\n- {nsfw}")
Redefining name 'app' from outer scope (line 1)
39
40# Usage: neko [-h|ARG]
41@app.on_message(filters.command("neko", prefix) & filters.me)
42async def getNeko(app, msg):43 url_api = "http://api.nekos.fun:8080/api/"
44 url_help = "https://www.nekos.fun/apidoc.html"
45 cmd = msg.command
Redefining name 'app' from outer scope (line 1)
4import requests
5
6@app.on_message(filters.command("git", prefix) & filters.me)
7async def git_user_info(app, msg): 8 url = "https://api.github.com/users/"
9 if len(msg.command) < 2:
10 await msg.edit(f"`{prefix}git` **username**")
Redefining name 'app' from outer scope (line 1)
5
6# exec os command and send output to message
7@app.on_message(filters.command(["exec",">"], prefix) & filters.me)
8async def execCommand(app, msg): 9 if len(msg.command) < 2:
10 await msg.edit("Usage: `exec [command]`")
11 return
Redefining name 'app' from outer scope (line 1)
4
5# get group id and send it to message
6@app.on_message(filters.command("id", prefix) & filters.me)
7async def getGroupId(app, msg): 8 if msg.chat.type == "private":
9 await msg.edit("This is private chat")
10 return
Redefining name 'app' from outer scope (line 1)
21 await msg.edit(f"Chat id: `{msg.chat.id}`\nUser forward message id: `{msg.reply_to_message.forward_from_message_id}`")
22# get user id and send it to message
23@app.on_message(filters.command("uid", prefix) & filters.me)
24async def getUserId(app, msg):25 if msg.reply_to_message:
26 await msg.edit(f"User id: `{msg.reply_to_message.from_user.id}`")
27 else:
Redefining name 'app' from outer scope (line 1)
28 await msg.edit("Reply to message")
29# get all id and send it to message
30@app.on_message(filters.command("allid", prefix) & filters.me)
31async def getAllId(app, msg):32 if msg.chat.type == "private":
33 await msg.edit("This is private chat")
34 return
Redefining name 'app' from outer scope (line 1)
39
40# get user info and send it to message
41@app.on_message(filters.command("info", prefix) & filters.me)
42async def getUserInfo(app, msg):43 if msg.reply_to_message:
44 await msg.edit(f"**User info**\n\n**First name:** `{msg.reply_to_message.from_user.first_name}`\n**Last name:** `{msg.reply_to_message.from_user.last_name}`\n**Username:** `{msg.reply_to_message.from_user.username}`\n**User id:** `{msg.reply_to_message.from_user.id}`")
45 else:
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()