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

  • Home
  • SEARCH
  • 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 9194265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:22:12+00:00 2026-06-17T21:22:12+00:00

Description : I’ve read a lot opinions about checking value type in python and

  • 0

Description:

I’ve read a lot opinions about checking value type in python and most said that you shouldn’t check type, but instead use try and except.
I want to make clear few things.

Questions:

When I have method like this:

def set_age(self, age):
    self._age = age

I can’t let user pass any types other then int because later in code it can cause some harder to debug bugs.
And according to Python methodology I can’t check type.
So how should I do it with try in the proper way?

def set_age(self, age):
    try:
        age = int(age)
    except TypeError:
        raise TypeError('First parameter must be of int type.')
    self._age = age

Or just

def set_age(self, age):
    self._age = int(age)

and let Python raise errors on it’s own.

I know it might be silly questions, I just want to make sure.

  • 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-06-17T21:22:13+00:00Added an answer on June 17, 2026 at 9:22 pm

    Python will raise an appropriate error if it’s not an integer, so there is no need to put your own over the top of it.

    Let’s step back a bit though, as there are other problems here. getters and setters are really unpythonic – either just use an attribute, or use properties.

    In this case, I think you are still thinking in the wrong way. The reason we don’t type check in Python is because we shouldn’t care what type something is, just if it will work in the given situation. Making everything the type you think it should be as soon as you get it is essentially type checking.

    You say it will make it harder to debug, but that’s not really the experience most people have. Duck typing is designed to allow anything that can do the job do the job. Converting everything to an int limits your program. The problem isn’t here, the problem is with your code that is passing something that doesn’t make sense to your class. In my experience, this is actually a really rare thing to do – you don’t tend to throw random stuff at classes and see what sticks.

    So, my suggestion here is that you use an attribute – just do x.age = ... instead of using set_age() or anything like that. If you really need to have an int (as in, only whole numbers make sense), then use a property and call int() in the setter, leaving the error to propagate to the caller if it happens.

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

Sidebar

Related Questions

Description By design most jquery code leads to a lot of tight coupling, e.g.
Description I have a bit of a problem with my project, that being said
Problem description: Read an xml file, traverse to a particular node (element), if it
Description: I am creating comment/reply functionality to a web app that I built. I
Description According to the explain command, there is a range that is causing a
Description: I have a column(EmailsAdress) on a table(BusinessUsers) on my databases that stores email
Initial description: My application handles some events and do some task related to that
Description: I have many dropdowns on my page that all call a update price
Description Resource Path Location Type No grammar constraints (DTD or XML schema) detected for
Description I have a expression to point on a property of my type. But

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.