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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:50:14+00:00 2026-05-15T08:50:14+00:00

This question is in continuation to my previous question, in which I asked about

  • 0

This question is in continuation to my previous question, in which I asked about passing around an ElementTree.

I need to read the XML files only and to solve this, I decided to create a global ElementTree and then parse it wherever required.

My question is:

Is this an acceptable practice? I heard global variables are bad. If I don’t make it global, I was suggested to make a class. But do I really need to create a class? What benefits would I have from that approach. Note that I would be handling only one ElementTree instance per run, the operations are read-only. If I don’t use a class, how and where do I declare that ElementTree so that it available globally? (Note that I would be importing this module)

Please answer this question in the respect that I am a beginner to development, and at this stage I can’t figure out whether to use a class or just go with the functional style programming approach.

  • 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-15T08:50:14+00:00Added an answer on May 15, 2026 at 8:50 am

    There are a few reasons that global variables are bad. First, it gets you in the habit of declaring global variables which is not good practice, though in some cases globals make sense — PI, for instance. Globals also create problems when you on purpose or accidentally re-use the name locally. Or worse, when you think you’re using the name locally but in reality you’re assigning a new value to the global variable. This particular problem is language dependent, and python handles it differently in different cases.

    class A:
       def __init__(self):
          self.name = 'hi'
    
    x = 3
    a = A()
    
    def foo():
       a.name = 'Bedevere'
       x = 9
    
    foo()
    print x, a.name #outputs 3 Bedevere
    

    The benefit of creating a class and passing your class around is you will get a defined, constant behavior, especially since you should be calling class methods, which operate on the class itself.

    class Knights:
       def __init__(self, name='Bedevere'):
           self.name = name
       def knight(self):
           self.name = 'Sir ' + self.name
       def speak(self):
           print self.name + ":", "Run away!"
    
    class FerociousRabbit:
       def __init__(self):
           self.death = "awaits you with sharp pointy teeth!"
       def speak(self):
           print "Squeeeeeeee!"
    
    def cave(thing):
       thing.speak()
       if isinstance(thing, Knights):
           thing.knight()
    
    def scene():
       k = Knights()
       k2 = Knights('Launcelot')
       b = FerociousRabbit()
       for i in (b, k, k2):
          cave(i)
    

    This example illustrates a few good principles. First, the strength of python when calling functions – FerociousRabbit and Knights are two different classes but they have the same function speak(). In other languages, in order to do something like this, they would at least have to have the same base class. The reason you would want to do this is it allows you to write a function (cave) that can operate on any class that has a ‘speak()’ method. You could create any other method and pass it to the cave function:

    class Tim:
       def speak(self):
           print "Death awaits you with sharp pointy teeth!"
    

    So in your case, when dealing with an elementTree, say sometime down the road you need to also start parsing an apache log. Well if you’re doing purely functional program you’re basically hosed. You can modify and extend your current program, but if you wrote your functions well, you could just add a new class to the mix and (technically) everything will be peachy keen.

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

Sidebar

Related Questions

This is in continuation to my previous question which was closed cause of similarity
This is a continuation of my previous question which I posted when I wasn't
This is the continuation of my previous question. deleting lines from text files based
This is a continuation question from a previous question I have asked I now
This is a continuation from the previous stackoverflow jQuery question I asked, but I
this is a continuation from a previous JQueryUI Autocomplete question, I asked . This
This question is a continuation of my previous question here zend models architecture (big
This question is in continuation to my previous question . I am trying to
This is a continuation of my previous question .NET regex engine returns no matches
Ok - this is in continuation from my earlier question about sending an email

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.