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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:27:36+00:00 2026-06-06T06:27:36+00:00

I have a function that performs a monthly task on one or more specified

  • 0

I have a function that performs a monthly task on one or more specified days (e.g., the first and the 15th.) For enhanced usability, I want to let users just pass in a single int when they only want it to happen one day each month, or a list of ints for multiple events per month.

monthly_event(days_of_month=1, event="paycheck") 
monthly_event(days_of_month=[1,15], event="bills", starting=date(2013,1,1))

Internally the function will iterate over the list and do the same thing it would do for a single int.

Since “int” is not an iterable, I need to do something to avoid a TypeError when the user just passes a single int. I was surprised to find that using an “or” expression and relying on short-circuiting does not work – the TypeError still happens. Here’s an example:

from datetime import date as date
dt = date.today()
days = 1
#days = [1,2]
if dt.day == days or dt.day in days:
    print "GOOD"
else:
    print "BAD"

My first question is: have I misunderstood Python, or does the internal typecheck really happen on the entire line of code before the short-circuiting of the boolean expression? That seems very strange.

My second question is: what is the Pythonic way to do this? I’d like to avoid doing an explicit type check on the “days” variable. Using a try/catch instead just bloats the code:

try:
    if dt.day == days:
        print "GOOD"
    else:
        print "BAD"
except TypeError:
    if dt.day in days:
        print "GOOD"
    else:
        print "BAD"

Is there something obvious I’ve overlooked?

  • 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-06T06:27:38+00:00Added an answer on June 6, 2026 at 6:27 am

    I believe the problem occurs when dt.day != days (and the short circuit is not taken), Python will then try the dt.day in days expression, and get a TypeError.

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

Sidebar

Related Questions

I have a function that does a long task and I want to update
Suppose I have a function that performs some task (this is in Python pseudocode):
I have a function that performs a very expensive computational task on a click
I need to process more than one function that goes and performs results returning
I have a javascript function that accepts a number and performs a mathematical operation
I have a function that performs a hierarchical clustering on a list of input
I have a .click() function that performs certain actions when a button is clicked.
I have a jQuery function that performs slideDown anitmation on a certain element when
I have a function that takes a pointer to a superclass and performs operations
To cut a long story short I have a C# function that performs a

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.