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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:49:16+00:00 2026-05-19T13:49:16+00:00

I can’t find the answer on SO but it’s very likely that the argument

  • 0

I can’t find the answer on SO but it’s very likely that the argument has been already discussed.

I’m trying to write a quite small size program using the Python language. It’s my first “real” experience with a dynamic language and I would prefer to do everything in the right way. One of the practice that I would try to apply since the beginning is unit-testing.

How can I quickly test that the parameters of a method are of the right type? Should I do it?

With right type I mean for instance to check that a method that works with float numbers is not called with a String. In this case consider the possibility that the method should obviously accept even integers and not only float.

  • 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-19T13:49:16+00:00Added an answer on May 19, 2026 at 1:49 pm

    How can I quickly test that the parameters of a method are of the right type?

    The quickest way is to do nothing.

    Seriously. The dynamic language interpreter (Python in this case) will check far more quickly than any code you could ever write. It will simply raise an exception and that’s all you need to do. Nothing.

    Should I do it?

    Never test for the right type. You can’t — in general — do it.

    Let’s say you have a function that requires a “number”

    def some_func( x ):
        assert isinstance(x, int)
    

    Bad policy. Your function may work for long or float just as well as int.

    assert instance( x, (int, long, float) )
    

    Bad policy. You’ve still excluded complex. Indeed, you’ve also excluded decimal.Decimal and fractions.Rational which are also valid numbers.

    By “type checking” you’re going to exclude valid types. The only thing you can do is assume the types are proper and handle the exception gracefully when someone “misuses” your function or class and provides wrong types.

    The most graceful way to handle TypeError?

    Do nothing. The program should totally crash.

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

Sidebar

Related Questions

Can I figure out if a function has already been assigned to an event?
Can a LINQ enabled app run on a machine that only has the .NET
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Can anyone help me trying to find out why this doesn't work. The brushes
can anyone help me in trying to check whether JavaScript is enabled in client
I have a jquery bug and I've been looking for hours now, I can't
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace

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.