Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 933137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:46:55+00:00 2026-05-15T20:46:55+00:00

I find this design pattern comes up a lot: try: year = int(request.GET[‘year’]) except:

  • 0

I find this design pattern comes up a lot:

try: year = int(request.GET['year'])
except: year = 0

The try block can either fail because the key doesn’t exist, or because it’s not an int, but I don’t really care. I just need a sane value in the end.

Shouldn’t there be a nicer way to do this? Or at least a way to do it on one line? Something like:

year = int(request.GET['year']) except 0

Or do you guys use this pattern too?


Before you answer, I already know about request.GET.get('year',0) but you can still get a value error. Wrapping this in a try/catch block to catch the value error just means the default value appears twice in my code. Even worse IMO.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T20:46:55+00:00Added an answer on May 15, 2026 at 8:46 pm

    I’d use a helper function:

    def get_int(request, name, default=0):
        try:
            val = int(request.GET[name])
        except (ValueError, KeyError):
            val = default
        return val
    

    then:

    year = get_int(request, 'year')
    

    It keeps the complexity of the try/catch in one place, and makes for tidy functions, where you have one line per parameter in your view functions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is another design pattern in some legacy code that I couldn't find much
I'm trying to find a useful design pattern that simulates something like this following:
I find this article to be useful for non-ajax request How to handle session
I find myself breaking this pattern all the time. YAGNI - You Ain't Gonna
Found myself trying to find a link to an official definition of this design
I am trying to find a ood example of MVC design pattern in java.
I'm trying to find a design pattern or a best practice, or some other
This is a design problem I face regularly and I'd like to find some
I find this feature of automatic selection to item index 0 in the Listbox
I find this pretty confusing. When you want to rotate a view, it's going

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.