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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:58:29+00:00 2026-06-14T03:58:29+00:00

Possible Duplicate: Auto-incrementing IDs for Class Instances I want to something like the following

  • 0

Possible Duplicate:
Auto-incrementing IDs for Class Instances

I want to something like the following Java class in Python:

public class MyObject {
    private static int ID = 0;
    private final int id;
    public MyObject() {
        id = ID++;
    }
}

In this Java code, every myObject will have id and there will be no way that two objects could have the same ID (it’s a one-threaded application).

Can I do something like this in Python?

  • 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-14T03:58:30+00:00Added an answer on June 14, 2026 at 3:58 am

    In Python, you can just refer directly to the class attribute:

    class MyObject(object):
        ID = 0
    
        def __init__(self):
           self.id = MyObject.ID = MyObject.ID + 1
    

    Demo:

    >>> class MyObject(object):
    ...     ID = 0
    ...     def __init__(self):
    ...        self.id = MyObject.ID = MyObject.ID + 1
    ... 
    >>> MyObject().id
    1
    >>> MyObject().id
    2
    >>> MyObject().id
    3
    >>> MyObject.ID
    3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: NullPointerException through auto-boxing-behavior of Java ternary operator The following code uses simple
Possible Duplicate: MySQL - Auto Increment after delete I want to delete the last
Possible Duplicate: JavaScript: Scroll Position of div with “overflow: auto” Hi, i want to
Possible Duplicate: Java conditional operator ?: result type NullPointerException through auto-boxing-behavior of Java ternary
Possible Duplicate: how to auto refresh MVC PartialView every second I want to refresh
Possible Duplicate: Access Auto-Increment Value During INSERT INTO Statement I want to run an
Possible Duplicate: Stop chrome from auto styling input type=search I want to add that
Possible Duplicate: How to set AUTO-SCROLLING of JTextArea in Java GUI? I'm creating an
Possible Duplicate: Access Auto-Increment Value During INSERT INTO Statement I would like to generate
Possible Duplicate: Auto-generation of .NET unit tests I am pretty new to Unit Testing

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.