Bird
Raised Fist0

Identify the error in this Python code snippet handling query parameter versioning:

medium📝 Debug Q6 of Q15
Rest API - Versioning Strategies
Identify the error in this Python code snippet handling query parameter versioning:
def get_version(request):
    version = request.GET.get('version')
    if version == None:
        version = 1
    return version
AThe comparison should be 'if version is None:'
BThe default version should be 0, not 1
CThe method GET.get() does not exist
DThe function should return a string, not an integer
Step-by-Step Solution
Solution:
  1. Step 1: Check None comparison in Python

    In Python, 'is None' is preferred over '== None' for None checks.
  2. Step 2: Validate other options

    Default 1 is valid, GET.get() is correct, returning int is fine.
  3. Final Answer:

    The comparison should be 'if version is None:' -> Option A
  4. Quick Check:

    Use 'is None' to check None in Python [OK]
Quick Trick: Use 'is None' to check None in Python [OK]
Common Mistakes:
MISTAKES
  • Using '== None' instead of 'is None'
  • Assuming GET.get() is invalid
  • Confusing default version number

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes