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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:12:40+00:00 2026-05-29T12:12:40+00:00

The basic idea of what I want to do is: def aFunction(string=”, dicti={}): if

  • 0

The basic idea of what I want to do is:

def aFunction(string='', dicti={}):
    if len(str) > 0:
         print('you gave string as input')
    if len(dicti) > 0:
         print('you gave a dict as input')

aFunction(string='test')
dict['test'] = test
aFunction(dicti=dict)

I know this kind of idea is possible in more OO type of languages, but is this also possible in Python?

Right now I’m doing

def aFunction(input):
    if type(input) == str:
         print('you gave string as input')
    if type(input) == dict:
         print('you gave a dict as input')

aFunction('test')

But I want the difference to be clear when the function is called.

  • 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-29T12:12:41+00:00Added an answer on May 29, 2026 at 12:12 pm

    Type checks should be avoided in Python (search about "duck typing" on that). When they are necessary, it should usually be done with isinstance rather than an equality check on the type like the question shows. This has the advantage of being more flexible for inheritance situations.

    Since Python 3.4, the string-like branch and the dict-like branch can be written in separate functions using stdlib functools.singledispatch.

    So instead of:

    def aFunction(input_):
        if isinstance(input_, str):
            print('you gave a string-like input')
            ...
        elif isinstance(input_, dict):
            print('you gave a dict-like input')
            ...
    

    You can now have:

    from functools import singledispatch
    
    @singledispatch
    def aFunction(input_):
        pass
    
    @aFunction.register(str)
    def _(input_):
        print("you gave a string-like input")
    
    @aFunction.register(dict)
    def _(input_):
        print("you gave a dict-like input")
    

    In Python 3.5+ you have another option of using type hinting function annotations. Read PEP 484 – Type Hints for more details about that feature. It means the single dispatch generic function above can be written as:

    from functools import singledispatch
    
    @singledispatch
    def aFunction(input_):
        pass
    
    @aFunction.register
    def _(input_: str):
        print("you gave a string-like input")
    
    @aFunction.register
    def _(input_: dict):
        print("you gave a dict-like input")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The basic idea is to take a string and evaluate it against an XML
I have a basic idea of HTML. I want to create the download link
the basic idea is that I want to link to path that's relative to
The basic idea is that I have a native function I want to call
Here is the basic idea of what I want to do: Each widget, when
I want to implement POSIX compliant microthreads in Linux environment. Basic idea is as
The basic idea behind a Cancel button is to enable closing your window with
The basic idea of a masterpage is simple -- you have a block of
the basic idea is that you have some class that has a reference type
Here's the basic idea: There is a java window (main) that opens another java

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.