55 case "GET":
56 match (uid):
57 case "upcoming":
58 meetings = ( 59 MeetingRoom.objects.filter(
60 time__gt=datetime.datetime.now(),
61 )
232 match request.method:
233 case "GET":
234 try:
235 profile = SocialProfile.objects.get(user=request.user)236 except SocialProfile.DoesNotExist:
237 return Response(
238 {"error": "User does not have a Social Profile"}, status=404
31 match request.method:
32 case "GET":
33 try:
34 profile = SocialProfile.objects.get(user=request.user) 35 except SocialProfile.DoesNotExist:
36 return Response(
37 {"error": "User does not have a Social Profile"}, status=404
67 match request.method:
68 case "GET":
69 try:
70 profile = SocialProfile.objects.get(user=request.user) 71 except SocialProfile.DoesNotExist:
72 return Response(
73 {"error": "User does not have a Social Profile"}, status=404
118 match request.method:
119 case "GET":
120 try:
121 profile = SocialProfile.objects.get(user=request.user)122 except SocialProfile.DoesNotExist:
123 return Response(
124 {"error": "User does not have a Social Profile"}, status=404
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()