Global variable declaration

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • psbasha
    Contributor
    • Feb 2007
    • 440

    #16
    Is there any declaration or defintion has to be done in the Sample1 and Sample2 modules?


    - PSB

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #17
      Originally posted by bartonc
      The mutable type to use between modules would be a class:
      Code:
      # test.py
      # class holds globals
      
      import inspect # just for testing this idea
      
      def PrintGlobRep():
          for member in inspect.getmembers(globRep):
              if not member[0].startswith('_'):
                  print member
      
      class GlobalRepository:
          globalList = []
          globalInt = 5
      
      
      globRep = GlobalRepository()
      That last line is special. It creates the global repository when the import is executed. It is accessed below by "from module import1". This is the best way to bring a mutable type variable for one module to another. Generally, though, this is done for "constants" (variables that should NEVER be assigned to).
      # module1.py
      Code:
      # module1 uses Global Repository from test.py
      
      import test # this does initialization of the Global Repository
      from test import globRep # get the class instance
      
      test.PrintGlobRep()
      globRep.newGlobal = 12
      test.PrintGlobRep()
      ('globalInt', 5)
      ('globalList', [])
      ('globalInt', 5)
      ('globalList', [])
      ('newGlobal', 12)

      Comment

      • psbasha
        Contributor
        • Feb 2007
        • 440

        #18
        Usually "global" variables in any programming languages are used very rarely, and as Barton said,we dont know where the value of the global value be changed.It will be very difficult to track the value of it.

        "global" variable main purpose is to make the life of the variable available from begining to the last of the program.

        Thanks
        PSB

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #19
          Originally posted by psbasha
          Usually "global" variables in any programming languages are used very rarely, and as Barton said,we dont know where the value of the global value be changed.It will be very difficult to track the value of it.

          "global" variable main purpose is to make the life of the variable available from begining to the last of the program.

          Thanks
          PSB
          That makes sense. Other options include database, config file, windows registry, etc. I uese the registry for startup values and SQL databse for shrare values.

          Comment

          • psbasha
            Contributor
            • Feb 2007
            • 440

            #20
            I tried as you suggested ,but still I am not able to solve the problem.

            Suggest me the location where I have to change in my sample code.

            Code:
            Main.py
            from Sample1 import CSample1
            from Sample2 import CSample2
            from Sample1 import iVar
            from Sample2 import iVar
            
            iVar = 1
            if __name__ == '__main__':
              
                
                obj1 = CSample1()
                obj2 = CSample2()
            
                obj1.Func1()
                obj2.Func4()
                print iVar
            Code:
            Sample1.py
            class CSample1:
            
                def Func1(self):
                    global iVar
                    iVar = 20
                    
                    print iVar
                
                def Func2(self):
                    pass
                
            iVar = CSample1()
            Code:
            Sample.2py
            class CSample2:
            
                def Func3(self):
                    pass
                
                def Func4(self):
                    global iVar
                    print iVar
            
            iVar = CSample2()
            -PSB

            Comment

            • psbasha
              Contributor
              • Feb 2007
              • 440

              #21
              Originally posted by psbasha
              I tried as you suggested ,but still I am not able to solve the problem.

              Suggest me the location where I have to change in my sample code.

              Code:
              Main.py
              from Sample1 import CSample1
              from Sample2 import CSample2
              from Sample1 import iVar
              from Sample2 import iVar
              
              iVar = 1
              if __name__ == '__main__':
                
                  
                  obj1 = CSample1()
                  obj2 = CSample2()
              
                  obj1.Func1()
                  obj2.Func4()
                  print iVar
              Code:
              Sample1.py
              class CSample1:
              
                  def Func1(self):
                      global iVar
                      iVar = 20
                      
                      print iVar
                  
                  def Func2(self):
                      pass
                  
              iVar = CSample1()
              Code:
              Sample.2py
              class CSample2:
              
                  def Func3(self):
                      pass
                  
                  def Func4(self):
                      global iVar
                      print iVar
              
              iVar = CSample2()
              -PSB
              I am getting the following output

              >>>
              20
              <Sample2.CSampl e2 instance at 0x00CF65F8>
              1
              >>>

              Comment

              • psbasha
                Contributor
                • Feb 2007
                • 440

                #22
                Any suggestions on the above posting.

                -PSB

                Comment

                • bvdet
                  Recognized Expert Specialist
                  • Oct 2006
                  • 2851

                  #23
                  Originally posted by psbasha
                  Any suggestions on the above posting.

                  -PSB
                  Code:
                  # Main.py
                  try:
                      reload(Sample1)
                  except:
                      import Sample1
                      
                  try:
                      reload(Sample2)
                  except:
                      import Sample2
                  
                  iVar = 1
                  if __name__ == '__main__':
                    
                      
                      obj1 = Sample1.CSample1()
                      obj2 = Sample2.CSample2()
                  
                      print iVar
                      iVar = obj1.Func1()
                      print iVar
                      iVar = obj2.Func4()
                      print iVar
                  
                  # Sample1.py
                  class CSample1:
                  
                      def Func1(self):
                          iVar = 40
                          return iVar
                      
                      def Func2(self):
                          pass
                  
                  # Sample.2py
                  class CSample2:
                  
                      def Func3(self):
                          pass
                      
                      def Func4(self):
                          iVar = 20
                          return iVar
                  >>> 1
                  40
                  20
                  >>>

                  It seems like a lot of trouble though.

                  Comment

                  • psbasha
                    Contributor
                    • Feb 2007
                    • 440

                    #24
                    Thanks for the reply

                    Is it not possible to get the results using the 'global'key word in different module?

                    -PSB

                    Comment

                    • bvdet
                      Recognized Expert Specialist
                      • Oct 2006
                      • 2851

                      #25
                      Originally posted by psbasha
                      Thanks for the reply

                      Is it not possible to get the results using the 'global'key word in different module?

                      -PSB
                      Yes.
                      Code:
                      try:
                          reload(Sample1)
                      except:
                          import Sample1
                          
                      try:
                          reload(Sample2)
                      except:
                          import Sample2
                      
                      iVar = 1
                      if __name__ == '__main__':
                        
                          
                          obj1 = Sample1.CSample1()
                          obj2 = Sample2.CSample2()
                      
                          print iVar
                          iVar = obj1.Func1()
                          print iVar
                          iVar = obj2.Func4()
                          print iVar
                          print Sample1.iVar
                      
                      # Sample1.py
                      class CSample1:
                      
                          def Func1(self):
                              global iVar
                              iVar = 40
                              return iVar
                          
                          def Func2(self):
                              pass
                      
                      a =  CSample1()
                      a.Func1()
                      >>> 1
                      40
                      20
                      40

                      Comment

                      Working...