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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:58:40+00:00 2026-05-15T17:58:40+00:00

I have a conceptual Python design dilemma. Say I have a City class, which

  • 0

I have a conceptual Python design dilemma.

Say I have a City class, which represents a city in the database. The City object can be initialized in two ways:

  1. An integer (actually, an ID of an existing city in a database)
  2. A list of properties (name, country, population, …), which will generate a new city in the database, and retrieve its ID.

This means that the City object will always have an ID – either the initialized ID or a newly-created ID derived from the database.

The classic Java approach would overload the constructor – One constructor would get a single intparameter, and the other would get numerous strongly-typed parameters.

I’ve failed to find an elegant way to do it in Python:

  • I can create a base class with a single method get_city_id, and derive CityFromID and
    CityFromNewData from it, but that’s a lot of effort to work around this language lacuna.
  • Using class methods seems awkward.
  • Using a constructor with a long list of parameters is also awkward: I’d put both city id and the alternatives, and verify within the method that that only a specific subset have values.

Using **kargs seems very inelegant, because the signature of the constructor does not clearly state the required input parameters, and docstrings just ain’t enough:

class City(object):
    def __init__(self, city_id=None, *args, **kargs):
        try:
            if city_id==None:
                self.city_id=city_id
            else:
                self.city_name=kargs['name']
        except:
            error="A city object must be instanciated with a city id or with"+\
            " full city details."
            raise NameError(error)

Is there a Pythonic, elegant solution to constructor overloading?

Adam

  • 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-15T17:58:40+00:00Added an answer on May 15, 2026 at 5:58 pm

    How about:

    class City(object):
       def __init__(self, name, description, country, populations):
          self.city_name = name
          # etc.
    
       @classmethod
       def from_id(cls, city_id):
           # initialise from DB 
    

    Then you can do normal object creation:

     >>> c = City('Hollowberg', '', 'Densin', 3)
     >>> c.id
     1233L
    
     >>> c2 = City.from_id(1233)
    

    ~~~~~~

    Also you might want to check out SQLAlchemy (and Elixir) for nicer ways to do these things

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

Sidebar

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.