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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:38:16+00:00 2026-05-15T23:38:16+00:00

In Python, I want to have a class attribute, a dictionary, with initialized values.

  • 0

In Python, I want to have a class attribute, a dictionary, with initialized values. I wrote this code:

class MetaDataElement:
    (MD_INVALID, MD_CATEGORY, MD_TAG) = range(3)
    mapInitiator2Type = {'!':MetaDataElement.MD_CATEGORY, 
                         '#':MetaDataElement.MD_TAG}

But when I try to run this code, I get an error message with “NameError: name ‘MetaDataElement’ is not defined”. Could you help me?

Thanks in advance.

  • 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-15T23:38:17+00:00Added an answer on May 15, 2026 at 11:38 pm

    You cannot refer to MetaDataElement while it is being constructed, since it does not yet exist. Thus,

    class MetaDataElement:
        (MD_INVALID, MD_CATEGORY, MD_TAG) = range(3)
        mapInitiator2Type = {'!':MetaDataElement.MD_CATEGORY, 
                             '#':MetaDataElement.MD_TAG}
    

    fails because the very construction of mapInitiator2Type requires MetaDataElement to have attributes, which it does not yet have. You can think of your constants MD_INVALID, etc. as variables that are local to the construction of your class. This is why the following works, as icktoofay wrote:

    class MetaDataElement:
        (MD_INVALID, MD_CATEGORY, MD_TAG) = range(3)
        mapInitiator2Type = {'!': MD_CATEGORY,  # MD_CATEGORY is like a local variable!
                             '#': MD_TAG}
    

    However, you can refer to the class MetaDataElement in any yet un-interpreted piece of code, as in

        def method_of_MetaDataElement(self):
            print MetaDataElement.MD_TAG
    

    You even have to refer to MetaDataElement, here, because MD_TAG is not a kind of local variable when method_of_MetaDataElement() is executed (MD_TAG was only defined as a kind of local variable during class construction). Once the class MetaDataElement is created, MD_TAG is simply a class attribute, which is why method_of_MetaDataElement() must refer to it as such.

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

Sidebar

Ask A Question

Stats

  • Questions 494k
  • Answers 494k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Setting the environment variable EDITOR to true before running git… May 16, 2026 at 11:06 am
  • Editorial Team
    Editorial Team added an answer That's E_FAIL, "Unspecified error". It's an utterly useless error code… May 16, 2026 at 11:06 am
  • Editorial Team
    Editorial Team added an answer It sounds like you want to group the MyObject instances… May 16, 2026 at 11:06 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

So I have this code in python that writes some values to a Dictionary
I have a Python class that have attributes named: date1, date2, date3, etc. During
okay code: #!/usr/bin/python import wx import sys class XPinst(wx.App): def __init__(self, redirect=False, filename=None): wx.App.__init__(self,
I have a bunch of Python modules I want to clean up, reorganize and
I have two Python lists of dictionaries, entries9 and entries10 . I want to
In Python, I have list of dicts: dict1 = [{'a':2, 'b':3},{'a':3, 'b':4}] I want
i've got a problem with python: I want to assign a method to an
Really confused about what's going on here. I have a class defined as follows:
I have the following model in Django: class Bout (models.Model): fighter_1 = models.ForeignKey(Fighter, related_name=bout_fighter_1)
I'd like to extract the text from an HTML file using Python. I want

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.