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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:51:23+00:00 2026-06-18T12:51:23+00:00

For my Python 2.7.3 project I have a class called custom_date , which has

  • 0

For my Python 2.7.3 project I have a class called custom_date, which has a property called fixed_date:

from datetime import date
class custom_date():
    def __init__(self, fixed_date):
        self.fixed_date = fixed_date

    def __lt__(self, other):
        return self.fixed_date < other

    #__gt__, __ge__, __le__, __eq__, __ne__ all implemented the same way

My idea is to be able to directly compare custom_date.fixed_date with the builtin date.

Problem

If I compare a custom_date object to a date object, it’s fine. However, if I compare a date object to a custom_date, it returns a TypeError

>>> from datetime import date
>>> x = custom_date(date(2013,2,1))
>>> y = date(2013,2,2)
>>> x > y
False
>>> y > x
TypeError: can't compare datetime.date to instance 

Is there any way around this?

  • 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-18T12:51:24+00:00Added an answer on June 18, 2026 at 12:51 pm

    Just subclass date to get this functionality. Since a datetime object is immutable, you need to use the __new__ constructor vs __init__:

    from datetime import date
    class custom_date(date):
        def __new__(cls, year,month,day):
            return date.__new__(cls, year, month,day)
    
    x = custom_date(2013,2,1)
    y = date(2013,2,2)
    
    print x<y
    print y<x
    

    Prints:

    True
    False
    

    Since the determinative class for comparisons is the LH class, the class on the left needs to have the correct comparison operators to deal with the comparison with the class on the right. If there are no comparison operators for either class, instances are sorted by identity — their memory address. Your error is essentially from trying to compare an apple to an orange: identity to a date class.

    Note that there used to be a rcmp that was removed in Python 2.1 to deal with such issues. Introduction of the new style classes and rich comparisons have also led to __cmp__ being deprecated.

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

Sidebar

Related Questions

I have a Python project that has the following structure: package1 class.py class2.py ...
I have a Visual Studio project which uses nmake to call a Python file
i have the following code in a module called code_database.py class Entry(): def enter_data(self):
I'm using the SQLAlchemy Python ORM in a Pylons project. I have a class
I have a bunch of Objects from the same Class in Python. I've decided
I have a python project that is using pylint and unittest. Unittest has me
I have a project in python that I am trying to run from IDLE
I am working on a GAE Python project which has items and transactions on
I have a project with 10 different python files. It has classes and functions
I'm writing a project for a Python coding class and I have a question.

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.