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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:48:39+00:00 2026-06-18T05:48:39+00:00

When a python module I’m writing is imported, I want to create a set

  • 0

When a python module I’m writing is imported, I want to create a set of attributes for the module based upon the contents of a dictionary defined in the same module. Here is a small portion of the dictionary from the module:

list_of_constellations = {
   0: Constellation("And", "Andromeda"),
   1: Constellation("Ant", "Antlia"),
   2: Constellation("Aps", "Apus"),
   3: Constellation("Aql", "Aquila"),
}

where Constellation is a namedtuple. What I want is to inject a new set of attributes into the namespace whose name is the first element in the tuple and whose value is the key. So, after importing, the following attributes can be used:

import constellations

print constellations.And   # prints 0
print constellations.Ant   # prints 1

How might I do this?

  • 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-18T05:48:40+00:00Added an answer on June 18, 2026 at 5:48 am

    In the module itself, the globals() function returns the module namespace as a dictionary; simply use the first element of each named tuple as the key to set the integer values:

    for key, const in list_of_constellations.items():
        globals()[const[0]] = v  # set "And" to 0, etc.
    

    or from outside the module, use setattr() to add attributes to the module:

    import constellations
    
    for key, const in constellations.list_of_constellations.items():
        setattr(constellations, constellation[0], v)  # set "And" to 0, etc.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a python module for a device that interacts with a user supplied
I have a Python Module test, I want to edit test and run some
When writing a Python module, is there a way to tell if the module
There is a python module called _Qt. PyQt is not what I want. I
I have a python module. I want to populate some values to it at
I have a python module which generates large data files which I want to
I'm writing a Python module that wraps out a certain web service API. It's
Is there a Python module that writes Excel 2007+ files? I'm interested in writing
I'm writing a python module in C and I need to have it call
I'm writing a Python module in C and I intend to mmap largeish blocks

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.