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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:06:39+00:00 2026-06-18T08:06:39+00:00

Shall same datatypes be used to return from functions, or assign default values ?

  • 0

Shall same datatypes be used to return from functions, or assign default values ? or None ? What is a better coding practice, and why ?

eg. Some pseudo codes in python :

/1

def my_position():   # returns a positive integer if found
    if(object is present):
          position = get_position()
          return position # eg 2,3,4,6
    else: 
          return None     # or return -1 or 0 ??

/2

def get_database_rows():    
    do query to whatever database
    if(rows are found):
       return [list of rows]
    else:
       return None  # or return empty list []  ?

/3

the_dictionary = {'a' : 'john','b':'mike','c': 'robert' }  # values are names i.e. non empty string
my_new_var = the_dictionary.get('z', None)  # or the_dictionary.get('z','')  ?
  • 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-18T08:06:40+00:00Added an answer on June 18, 2026 at 8:06 am
    1. Raise an IndexError if the item is not found. This is what Python’s list does. (Or maybe return the index where the item should have lived when doing a binary search or similar operation.)

    2. Think about what your function does, logically: if it returns a list of all items in a DB that satisfy some criterion, and there are no such items, then it makes sense to return an empty list since that allows all the usual list operations (len, in) to function without the need for an explicit check.

      However, if the absence of the required items indicates inconsistency, then raise an exception.

    3. My previous remark applies especially to this case: it depends on what you’re going to do with the value you get. An ordinary dict just raises a KeyError when a key is not found. You’re replacing that exception with a value, so you should know which value makes sense in the context of your program. If no value does, then just let the exception fly.

    That said, returning None is often a bad idea because it may obscure bugs. None is the default return value in Python, so a function returning it may indicate nothing more than its author’s forgetting a return statement:

    def food(what):
        if what == HAM:
            return "HAM!"
        if what == SPAM:
            return " ".join(["SPAM" for i in range(10)])
        # should raise an exception here
    
    lunch = food(EGGS)    # now lunch is None, but what does that mean?
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This method shall take two objects of the same type and return one of
I have a form in which people shall be able to add the same
Q1 Why do new classes from .NET implement interfaces only partially? Q2 Shall I
i have to develop a wpf control which shall have the same behaviour as
Here's some background: I need a counter variable shared between go routines, used for
In C99 6.2.5 P27 All pointers to structure types shall have the same representation
There is an existing iphone app, but its boring. However the same app shall
I need to make app with will fill array with some random values, but
$10.3/5 The return type of an overriding function shall be either identical to the
from c++11 29.3-p3: There shall be a single total order S on all memory_order_seq_cst

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.