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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:19:13+00:00 2026-06-01T22:19:13+00:00

Lacking experience with maintaining dynamic-typed code, I’m looking for the best way to handle

  • 0

Lacking experience with maintaining dynamic-typed code, I’m looking for the best way to handle this kind of situations :

(Example in python, but could work with any dynamic-typed language)

def some_function(object_that_could_be_a_list):
     if isinstance(object_that_could_be_a_list, list):
          for element in object_that_could_be_a_list:
              some_function(element)
     else:
          # Do stuff that expects the object to have certain properties 
          # a list would not have

I’m quite uneasy with this, since I think a method should do only one thing, and I’m thinking that it is not as readable as it should be. So, I’d be tempted to make three functions : the first that’ll take any object and “sort” between the two others, one for the lists, another for the “simple” objects. Then again, that’d add some complexity.

What is the most “sustainable” solution here, and the one that guarantee ease of maintenance ? Is there an idiom in python for those situations that I’m unaware of ? Thanks in advance.

  • 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-01T22:19:15+00:00Added an answer on June 1, 2026 at 10:19 pm

    Don’t type check – do what you want to do, and if it won’t work, it’ll throw an exception which you can catch and manage.

    The python mantra is ‘ask for forgiveness, not permission‘. Type checking takes extra time, when most of the time, it’ll be pointless. It also doesn’t make much sense in a duck-typed environment – if it works, who cares why type it is? Why limit yourself to lists when other iterables will work too?

    E.g:

    def some_function(object_that_could_be_a_list):
        try:
            for element in object_that_could_be_a_list:
                some_function(element)
        except TypeError:
            ...
    

    This is more readable, will work in more cases (if I pass in any other iterable which isn’t a list, there are a lot) and will often be faster.

    Note you are getting terminology mixed up. Python is dynamically typed, but not weakly typed. Weak typing means objects change type as needed. For example, if you add a string and an int, it will convert the string to an int to do the addition. Python does not do this. Dynamic typing means you don’t declare a type for a variable, and it may contain a string at some point, then an int later.

    Duck typing is a term used to describe the use of an object without caring about it’s type. If it walks like a duck, and quacks like a duck – it’s probably a duck.

    Now, this is a general thing, and if you think your code will get the ‘wrong’ type of object more often than the ‘right’, then you might want to type check for speed. Note that this is rare, and it’s always best to avoid premature optimisation. Do it by catching exceptions, and then test – if you find it’s a bottleneck, then optimise.

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

Sidebar

Related Questions

I am new to LINQ.I used Strongly Typed DatSet. What is lacking in Typed
The recommended way to handle optimistic locking in a RESTful interface seems to be
Note that this is not about auto-completion or the excellent code formatter, Alt+Shift+F which
Forgive my somewhat-lacking ASP.NET knowledge for this question :) Here's the scenario: I'm playing
What are good ways to handle user input concurrency? As the answers to this
I have created a PHP script and I am lacking to extract the primary
Being a hobbyist coder, I'm lacking some fundamental knowledge. For the last couple days
I am a web developer so my knowledge of manipulating mass data is lacking.
I am making my own installer and it's almost complete. The only thing lacking
Why is locking a type considered very bad? For example, lock(typeof(DateTime)) I understand that

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.