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

  • Home
  • SEARCH
  • 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 7755793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:39:12+00:00 2026-06-01T12:39:12+00:00

I have a container class for urls and their headers, Resource: class Resource(object): def

  • 0

I have a container class for urls and their headers, Resource:

class Resource(object):
    def __init__(self, url, headers):
        self.url = url
        self.content-length = headers['content-length']
        self.content-type = headers['content-type']

        # etc....

The headers argument to the __init__ method expects a dict returned from the getinfo() method of urllib2.urlopen(). I thought this would be a more readable way of packaging up the resource url and headers. Typing self.someheader = headers['someheader'] over and over made me wonder if there’s some way to automate creating variables from dictionary keys like this. Is this possible?

  • 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-01T12:39:13+00:00Added an answer on June 1, 2026 at 12:39 pm

    Python identifiers can’t have the - symbol in them! So we can replace it with _ in the keys.
    Changing the keys to lowercase makes them look like a conventional variable name, and, what is very important, removes uppercase/lowercase confusion (because headers are usually sent with capitalized keys).

    for k, v in headers.items():
        setattr(self, k.lower().replace('-', '_'), v)
    

    If you’re using Python 2, iteritems is better here, as it doesn’t create a new list of items, but just lets you iterate over them, which Python 3 does by default.

    It may be a good idea to store those keys in a “private” dict (e. g. self._headers). Then you can have much more control over the process with __getattr__ and __setattr__, for example, an exception can be raised during an attempt to set an invalid key.

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

Sidebar

Related Questions

I have a container class which uses boost::optional to hold the value. Here is
I have a templated container class that looks something like this: template <class ItemType>
I have a custom container class for which I'd like to write the iterator
I have the following model structure: class Container(models.Model): pass class Generic(models.Model): name = models.CharacterField(unique=True)
I have code that looks like this: class T {}; class container { const
All, I have a simple class. public class Container : UserControl { public bool
I have a class with a std::map used as a container. I want to
I have tabs that calls via javascript urls of django to complete the container
I have a container class that has a generic parameter which is constrained to
I have implemented my own container class and need to implement a const_iterator for

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.