Performance: Parameter type converters (int, float, path)
This affects how Flask routes parse URL parameters and impacts request handling speed and routing efficiency.
from flask import Flask app = Flask(__name__) @app.route('/user/<int:user_id>') def user_profile(user_id): return f"User ID is {user_id}"
from flask import Flask app = Flask(__name__) @app.route('/user/<username>') def user_profile(username): user_id = int(username) # converting inside the view return f"User ID is {user_id}"
| Pattern | Routing Speed | Parameter Conversion | Error Handling | Verdict |
|---|---|---|---|---|
| Manual conversion inside view | Slower (conversion after routing) | Repeated per request | Errors handled late | [X] Bad |
| Using Flask converters in route | Faster (conversion during routing) | Single, automatic | Errors handled early | [OK] Good |
int converter do in a Flask route parameter like <int:id>?int converterint converter tells Flask to accept only integer values for that URL part.price?float converter is used to accept decimal numbers in the URL.<float:price> inside the route decorator.@app.route('/files/<path:filename>')
def show_file(filename):
return f"File requested: {filename}"/files/docs/manual.pdf?path converter behaviorpath converter matches the entire remaining path including slashes./files/docs/manual.pdf matches filename as docs/manual.pdf.@app.route('/product/<int:product_id>/<float:price>')
def show_product(product_id, price):
return f"Product {product_id} costs {price}"/product/abc/19.99, what error occurs?int converter requirement/download/docs/manual/2. Which route definition correctly uses converters to achieve this?/download/ followed by a path (which may include slashes), then a slash, then an integer version.path for filepath and int for version in correct order/download/<path:filepath>/<int:version> to match the URL structure.Pass the test and this pattern moves unknown