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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:26:43+00:00 2026-06-13T18:26:43+00:00

I have a question similar to the question asked here: Importing a long list

  • 0

I have a question similar to the question asked here: Importing a long list of constants to a Python file

Basically, I have a separate module which just contains a long list of constants, e.g.

constants.py

x = 1.2
y = 30.4
.
.
.

In another module, I would like import these constants and initialize them as instance attributes.

class.py

class something(object):
    def __init__(self):
        import constants
        self.x = constants.x
        self.y = constants.y
        .
        .
        .

Is there an easier or more pythonic way to do this instead of retyping all of the variable names?

  • 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-13T18:26:45+00:00Added an answer on June 13, 2026 at 6:26 pm

    Python developers have developed a cool solution: it’s called ConfigParser.

    A practical example: config.ini

    [constants]
    key = value
    x = 0
    y = 1
    

    class.py

    from ConfigParser import SafeConfigParser
    class MyClass(object):
        def __init__(self, **kwargs):
            self.config = SafeConfigParser(defaults=kwargs).read('config.ini')
    
        def get_x(self):
            return self.config.get('constants', 'x')
    

    I hope this helps.

    EDIT: If you need instance attributes, you can do:

    class MyClass(object):
        ...
        def __getattr__(self, key):
            return self.config.get('constants', key)
    

    EDIT2: need a Python file and a .ini file is not a solution?

    from . import config
    class Myclass(object):
        def __getattr__(self, key):
            return getattr(config, key)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have already asked a similar question here: Python open raw audio data file
Somebody asked similar question not long ago. But nobody answered comprehensively. Assume I have:
Similar question have been asked here but none fits to my need. I made
I have asked a similar question before here , but after much thought, and
I have asked a similar question here and got some answers, so first of
I have asked similar question for Linux RPM ( Adding License Agreement in RPM
I have asked a question similar to this in the past but this is
I have asked a similar question about command line arguments in C++ some hours
I have a similar question to the one I just asked. I'm trying to
(Before I start, yes I have asked a similar question before; unfortunately due to

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.