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 7608701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:59:07+00:00 2026-05-31T00:59:07+00:00

Lately, I’ve been adding asserts to nearly every single function I make to validate

  • 0

Lately, I’ve been adding asserts to nearly every single function I make to validate every input as sort of a poor-man’s replacement for type checking or to prevent myself from accidentally inputting malformed data while developing. For example,

def register_symbol(self, symbol, func, keypress=None):
    assert(isinstance(symbol, basestring))
    assert(len(symbol) == 1)
    assert(callable(func))
    assert(keypress is None or type(keypress) is int)
    self.symbols_map[symbol] = (func, keypress)
    return

However, I’m worried that this goes against the idea of duck typing, and that I might be going too overboard or constricting myself unnecessarily. Can you ever have too many assert statements? When’s a good time to stop?

  • 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-31T00:59:08+00:00Added an answer on May 31, 2026 at 12:59 am

    I only use asserts if they provide far better diagnostics than the error messages that I would get otherwise. Your third assert

    assert(callable(func))
    

    might be an example for such an assert — if func is not callable, you will get an error message at a completely different line of code than where the actual error is, and it might not be obvious how the non-callable object ended up in self.symbols_map. I write “might” because this depends on the rest of your code — if this is the only place where self.symbols_map gets updated, the assert might also be unnecessary.

    The first and last assert definitely are against the idea of duck-typing, and the second one is redundant. If symbol isn’t a string of length 1, chances are that self.symbols_map[symbol] will raise a KeyError anyway, so no need for the asserts.

    The last assert is also wrong — type(keypress) cannot be None, and type checks should be done with isinstance(). There might be very specialised applications where you cannot allow subtypes, but than the check should be performed with type(x) is int instead of type(x) == int. Checking for None should be done by x is None, not by type(x) is NoneType.

    You should probably write a good set of unit tests — they will be far more useful than the asserts, and might make almost all of your asserts redundant.

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

Sidebar

Related Questions

Lately i've been through a lot of times on a single situation problem: I
Lately I've been in the habit of assigning integer values to constants and simply
Lately I've been using XPathDocument and XNavigator to parse an XML file for a
Lately I've been taking a look at Haxe , to build an application to
Lately, I've been going through a pretty weird phase. I feel the need to
Lately I have been playing a game on my iPhone called Scramble. Some of
Lately I have been playing around with intents and bundles. I thought I had
Lately I've been working on stored procedure and encountered 1 strange problem. First, I
Lately, I've been reading much about constructors from the well-received C++ FAQ . One
Lately something weird has been happening to my projects in xcode: I've been trying

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.