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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:24:23+00:00 2026-06-10T00:24:23+00:00

In the below code, the compiler is giving me the error Local variable shouldStoreData

  • 0

In the below code, the compiler is giving me the error “Local variable shouldStoreData” referenced before assignment. But I am assigning it first.

class MyHTMLParser(HTMLParser):
    shouldStoreData = False
    textPartDoc = ""
    def handle_starttag(self, tag, attrs):
        print "Encountered a start tag:", tag
        if(tag == "title" or tag == "body"):
            shouldStoreData = True
    def handle_endtag(self, tag):
        print "Encountered an end tag :", tag        
    def handle_data(self, data):
        #print "Data is",data
        #print "valus of storeData is:",shouldStoreData
        if(shouldStoreData == True):
            textPartDoc  = textPartDoc + " " + data
            shouldStoreData = False

What I am trying to do here is that I want to store the data only when the tag is title or any tag within body.

  • 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-10T00:24:24+00:00Added an answer on June 10, 2026 at 12:24 am

    The first shouldStoreData is a class attribute. The ones inside handle_starttag and handle_data are local variables. In handle_data, you test the variable before you assign it. Code inside the method doesn’t know that the class attribute exists unless you explictly access it as a class/instance attribute (e.g., self.shouldStoreData or MyHTMLParser.shouldStoreData).

    If you want shouldStoreData to be an instance attribute, then you need to write an __init__ method and create it as an instance attribute:

    def __init__(self):
        self.shouldStoreData = False
    

    and then later refer to it as self.shouldStoreData in your other methods.

    You should read the Python tutorial to get a grasp of the basics of classes, attributes, and methods in Python.

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

Sidebar

Related Questions

The following code generates the compiler error below (after the code), but not if
I have presented the code below. The compiler throws error when I overload an
It is a compiler error or runtime error? The code below can be compiled!
Below code instantiates a derived singleton object based on environment variable. The compiler errors
whats wrong with the below code, its throwing me an error of Compiler Error
Why is the below code giving a compile time error. Map<String,? extends Object> inputMap
Given the code below, the compiler is showing a message pointing that error: templates
the code below produces compiler error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)'
The below code couldn't pass the compilation, what's the consideration for this compiler error?
when i compile the code below the compiler give the error:A field initializer cannot

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.