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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:49:02+00:00 2026-05-15T05:49:02+00:00

here is post my code:this is no the entire code but enough to explain

  • 0

here is post my code:this is no the entire code but enough to explain my doubt.please discard any code line which u find irrelavent

enter code here
saving_tree={}   
isLeaf=False
class tree:
    global saving_tree
    rootNode=None
    lispTree=None
    def __init__(self,x):
        file=x
        string=file.readlines()
        #print string
        self.lispTree=S_expression(string)
        self.rootNode=BinaryDecisionNode(0,'Root',self.lispTree)

class BinaryDecisionNode:
    global saving_tree     
    def __init__(self,ind,name,lispTree,parent=None):

        self.parent=parent

        nodes=lispTree.getNodes(ind)
        print nodes
        self.isLeaf=(nodes[0]==1)
        nodes=nodes[1]#Nodes are stored
        self.name=name
        self.children=[]
        if self.isLeaf: #Leaf Node
            print nodes
            #Set the leaf data
            self.attribute=nodes
            print "LeafNode is ",nodes
        else:            
            #Set the question
            self.attribute=lispTree.getString(nodes[0])
            self.attribute=self.attribute.split()


            print "Question: ",self.attribute,self.name
            tree={}
            tree={str(self.name):self.attribute}
            saving_tree=tree
            #Add the children
            for i in range(1,len(nodes)):#Since node 0 is a question
               # print "Adding child ",nodes[i]," who has ",len(nodes)-1," siblings"
                self.children.append(BinaryDecisionNode(nodes[i],self.name+str(i),lispTree,self))

        print saving_tree

i wanted to save some data in saving_tree{},which i have declared previously and want to use that saving tree in the another function outside the class.when i asked to print saving_tree it printing but,only for that instance.i want the saving_tree{} to have the data to store data of all instance and access it outside.
when i asked for print saving_tree outside the class it prints empty{}..
please tell me the required modification to get my required output and use saving_tree{} outside the class..

  • 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-15T05:49:03+00:00Added an answer on May 15, 2026 at 5:49 am

    saving_tree is not global in the __init__ method (which is a different scope than the class body). You could fix that by adding global saving_tree as the first statement in the method (and remove that in the body which plays no role).

    A better approach would be to forget about global and use a class attribute instead:

    class BinaryDecisionTree(object):
        saving_tree = None
        def __init__ ...
            ...
            BinaryDecisionTree.saving_tree = ...
    

    globals are always, at best, a so-so approach, and one of the advantages of moving to OOP (object oriented programming, i.e., class statements) is that it saves any need for global as you can always use class or instance attributes instead.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Spring Security 3.0 has this API SecurityContextHolderAwareRequestWrapper.isUserInRole(String role) You'll have… May 15, 2026 at 7:04 am
  • Editorial Team
    Editorial Team added an answer You didn't specify how you're including those image resources into… May 15, 2026 at 7:04 am
  • Editorial Team
    Editorial Team added an answer The best way to do this would be by using… May 15, 2026 at 7:04 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

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.