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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:31:46+00:00 2026-05-26T20:31:46+00:00

What is a clean way to declare multiple constructors for one class? For example,

  • 0

What is a clean way to declare multiple constructors for one class?

For example, let’s say I have an Item class. One way to create an Item (for example is)

item = Item(product_id, name, description,price)

another way to do the same thing might

item = Item(otherItem)

And then another way to do this.. maybe in some cases I don’t have the price so I want to pass just

item = Item(product_id, name,description)

and yet another case might be

item = Item(product_id,price)

The other question I have is:
There are some private variables which might be initialized during runtime.
Let’s say I have some random variable itemCount and I want to keep a track of it internally.

How do I declare it that I dont have to put it in initialization mode, but rather, somewhere in the run time..
I can do something like

self._count +=1

Thanks

  • 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-05-26T20:31:47+00:00Added an answer on May 26, 2026 at 8:31 pm

    You could use default arguments:

    class Item(object):
    
      def __init__(self, product_id = None, name = None, description = None, price = None)
          ... implementation ...
    

    You can substitute in any value you want for None, if the default value should be something different.

    Example usage:

    item1 = Item(product_id = 4, price = 13) # use the field name!
    item2 = Item(name = "hammer", description = "used to belong to Thor")
    

    For the copy constructor, item = Item(otherItem), @Raymond’s suggestions of class methods and factory functions may be the most Pythonic way to go.


    Update: here’s a question about multiple constructors in Python. It also mentions using *args and **kwargs.

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

Sidebar

Related Questions

I'm looking for a clean way to import one git repository from another, where
This is one of those there's gotta be a better way questions. Let me
I have an abstract base class and I want to declare a field or
Is there a clean way of cloning a record in SQL that has an
Is there a clean way to extract the VBA from a spreadsheet and store
Is there an easy/clean way to do this in Linux/ a Linux-like environment? Purpose
Can you recommend efficient/clean way to manipulate arbitrary length bit array? Right now I
I am trying to figure out a clean way to intercept uncaught exceptions that
I am looking for a clean way to check if a registry key exists.
I am looking for a neat/clean way to store a list of strings into

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.