sharing variables between two scripts

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vishalsethia
    New Member
    • Feb 2007
    • 7

    #1

    sharing variables between two scripts

    I was wondering if I declare a variable as global , can I share the same variable in a different file with the updated value. (ie) A global variable value to be used between different files without passing them within functions.


    Say for eg.

    Script 1
    Code:
    global foo = "sample"
    
    def func1
       global foo
       foo = "updated sample"
       print foo 
    
    print foo
    func1 
    x = script2.script2()
    print foo
    Script 2 (filename - script2)
    ------------
    Code:
    class script2:
         def __init__():
              global foo
              print foo
              foo = "Updated in Script2 "
    Is there a better way in which I use variables among different files without declaring them as global ?

    Thanks
    --V
    Last edited by bartonc; Feb 6 '07, 01:29 AM. Reason: added [code][/code] tags
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    It works like this:

    Code:
    # script1.py
    a = 1 # this is module scope variable
    def func1():
        pass    # here a function in script1 is defined
    
    class myClass(object):
        pass    # difine a class
    Code:
    # script2.py
    import script1
    print script1.a
    
    script1.func1()
    
    myInst = script1.myClass()

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by bartonc
      It works like this:

      Code:
      # script1.py
      a = 1 # this is module scope variable
      def func1():
          pass    # here a function in script1 is defined
      
      class myClass(object):
          pass    # difine a class
      Code:
      # script2.py
      import script1
      print script1.a
      
      script1.func1()
      
      myInst = script1.myClass()
      You can also (but this is discouraged)
      Code:
      # script2.py
      from script1 import *
      print a
      or (this is better practice)
      Code:
      # script2.py
      from script1 import myClass
      myInst = myClass()
      This is done to get constants, functions and classes. Actual variables always stay in the module where they are changed. This is part of what makes programming in python make sense. Globals are discourages as a generaly rule, but there are times when they are needed.

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by vishalsethia
        I was wondering if I declare a variable as global , can I share the same variable in a different file with the updated value. (ie) A global variable value to be used between different files without passing them within functions.


        Say for eg.

        Script 1
        Code:
        global foo = "sample"
        
        def func1
           global foo
           foo = "updated sample"
           print foo 
        
        print foo
        func1 
        x = script2.script2()
        print foo
        Script 2 (filename - script2)
        ------------
        Code:
        class script2:
             def __init__():
                  global foo
                  print foo
                  foo = "Updated in Script2 "
        Is there a better way in which I use variables among different files without declaring them as global ?

        Thanks
        --V
        By the way, welcome to the Python Forum on TheScripts.com. You'll learn to use code tags as you go along. There are a couple of places to find "Posting Guidelines". Just ask if you need help and keep posting,
        Barton

        Comment

        • vishalsethia
          New Member
          • Feb 2007
          • 7

          #5
          I tried to implement based on the previous comment, but it doesn't seem to serve to work. I am not sure what am I missing here ?

          The output I get is

          ['234', '123']
          ['444', '233']
          []

          I am not sure how do I achieve this ?
          If either of the files update that global variable, wherever I print, it should have that updated value. Also I am not sure declaring global variables is a nice idea for sharing variables among files.

          Script 1
          --------------
          Code:
          from foo import MyClass
          
          myinst = MyClass()
          print myinst.alist
          myinst.alist = ['444','233']
          print myinst.alist
          myinst.func1()


          Script 2
          ------------
          Code:
          alist = []
          
          
          class MyClass(object):
                  alist = ['234','123']
                  pass
          
                  def func1(self):
                          print alist
                          pass


          Thanks

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by vishalsethia
            I tried to implement based on the previous comment, but it doesn't seem to serve to work. I am not sure what am I missing here ?
            I am not sure how do I achieve this ?
            If either of the files update that global variable, wherever I print, it should have that updated value. Also I am not sure declaring global variables is a nice idea for sharing variables among files.
            Thanks
            Yes, declaring global variables is not a nice idea for sharing variables among files. You are on the right track by using classes to hold variables. Keep playing with that and keep posting.

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              Originally posted by vishalsethia
              I tried to implement based on the previous comment, but it doesn't seem to serve to work. I am not sure what am I missing here ?

              The output I get is

              ['234', '123']
              ['444', '233']
              []

              I am not sure how do I achieve this ?
              If either of the files update that global variable, wherever I print, it should have that updated value. Also I am not sure declaring global variables is a nice idea for sharing variables among files.

              Script 1
              --------------
              Code:
              from foo import MyClass
              
              myinst = MyClass()
              print myinst.alist
              myinst.alist = ['444','233']
              print myinst.alist
              myinst.func1()


              Script 2
              ------------
              Code:
              alist = []
              
              
              class MyClass(object):
                      alist = ['234','123']
                      pass
              
                      def func1(self):
                              print alist
                              pass


              Thanks
              'alist' in Script2 is defined twice - first as a global variable and second as a class variable. 'func1' looks inside its scope first for alist. Not finding it, it looks in the global scope and finds an empty list '[]' to print.
              Code:
              class MyClass(object):
                      alist = ['234','123']
                      pass
              
                      def func1(self):
                              print self.alist
                              pass
              'func1' is now an instance method and finds the class variable 'alist' to print:
              >>> ['234', '123']
              ['444', '233']
              ['444', '233']

              I try to avoid global variables where possible.

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                Originally posted by bvdet
                'alist' in Script2 is defined twice - first as a global variable and second as a class variable. 'func1' looks inside its scope first for alist. Not finding it, it looks in the global scope and finds an empty list '[]' to print.
                Code:
                class MyClass(object):
                        alist = ['234','123']
                        pass
                
                        def func1(self):
                                print self.alist
                                pass
                'func1' is now an instance method and finds the class variable 'alist' to print:
                >>> ['234', '123']
                ['444', '233']
                ['444', '233']

                I try to avoid global variables where possible.
                Clarification - 'func1' now finds the class variable 'alist' to print. The global namespace for a function is always the module in which it is defined.
                Script2:
                Code:
                alist = ['1', '2', '3']
                
                class MyClass(object):
                        alist = ['234','123']
                        pass
                
                        def func1(self):
                                print self.alist
                                pass
                >>> import Script2
                >>> Script2
                <module 'Script2' from 'C:\SDS2_7.0\ma cro\Work In Progress\Script 2.py'>
                >>> Script2.alist
                ['1', '2', '3']
                >>> myinst
                <Script2.MyClas s object at 0x00D66FF0>
                >>> myinst.alist
                ['444', '233']
                >>> myinst.func1()
                ['444', '233']
                >>>

                HTH :)

                Comment

                • vishalsethia
                  New Member
                  • Feb 2007
                  • 7

                  #9
                  Thanks .. that helped.

                  Comment

                  Working...