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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:48:44+00:00 2026-06-06T23:48:44+00:00

I want a Class attribute which sets datetime.now() when the a new instance of

  • 0

I want a Class attribute which sets datetime.now() when the a new instance of the class is instantiated. With this code, MyThing.created seems to always be when MyThing is imported, as opposed to when mt is instantiated.

from datetime import datetime

class MyThing:
    __init__(self, created=datetime.now()):
        self.created = created

MyThing.created
datetime.datetime(2012, 7, 5, 10, 54, 24, 865791)

mt = MyThing()
mt.created
datetime.datetime(2012, 7, 5, 10, 54, 24, 865791)

How can I do it so that created is when mt is instantiated, as opposed to MyThing?

  • 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-06T23:48:45+00:00Added an answer on June 6, 2026 at 11:48 pm

    Default values for function parameters are computed once, when the function is defined. They are not re-evaluated when the function is called. Typically, you’d use None as the default value, and test for it in the body:

    def __init__(self, created=None):
        self.created = created or datetime.now()
    

    Even better, it looks like maybe you don’t want to ever pass a created date into the constructor, in which case:

    def __init__(self):
        self.created = datetime.now()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple images that have the same class attribute and I want to
I want to define some properties on a class using the [Indexable()] attribute in
I want to know how to create a private attribute in a Javascript class.
I'm working on a new class to wrap XML handling. I want my class
In the code base I was maintaining I found this exact class, pasted below.
I have a class Attribute which has 2 variables say int a,b ; I
I have an aggregated attribute which I want to be able ask about its
I want to apply an attribute which accepts a delegate as an argument, but
I have a custom attribute which can be assigned to a class, [FooAttribute] .
I would like to initialize a class, then call an attribute which was set

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.