Accessing variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kaarthikeyapreyan
    New Member
    • Apr 2007
    • 106

    Accessing variables

    Is there any possibility that i could access a the variables that i have defined in a function
    for example
    Code:
    def foo():
      i = 10
      i = i + 10
    I would like to retrieve the value and use it in another function
    I was able to find a way accessing the variables declared in methods
    my problem is when i do it with functions and
    how does python work with the scope of the variables
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    You can return variables like that with the 'return' keyword, which also ends the function when it's returned. To return multiple values at once, you need to use a list/tuple. You'll need to assign the output of the function to another variable.

    Comment

    • heiro
      New Member
      • Jul 2007
      • 56

      #3
      Here is the example.
      Code:
      def foo():
           i = 10
           i = i + 10
           return i
      print foo()

      Comment

      • kaarthikeyapreyan
        New Member
        • Apr 2007
        • 106

        #4
        Sorry about not being so clear
        my actual question goes this way

        I define two functions

        Code:
        def foo():
          i=10
        Code:
        def bar():
          sum = 10 + i # the i should be from the function foo
        i would not prefer to use the return function cause i am returning something else with it
        to be more precise this is how i do it for a method variable

        Code:
        >>> class a:
        ...   def foo(self):
        ...     self.i = 10
        ...     self.i = self.i + 10
        ... 
        >>> obj = a()
        >>> obj.foo()
        >>> obj.i
        20
        i could manipulate the value of i using the class instance
        similarly how do i do it for variables inside a function

        Comment

        • Laharl
          Recognized Expert Contributor
          • Sep 2007
          • 849

          #5
          The easiest way to do this is to declare i in global scope and then use the global keyword to bring it in. This does bring up some scoping issues I'm not entirely familiar with...

          [CODE=python]
          def foo():
          global i
          i += 10
          i = 0
          foo()
          print i #prints 10
          [/code]

          Comment

          • AIProgrammer
            New Member
            • Jul 2008
            • 28

            #6
            Hi,
            Your problem has two solutions:
            SOULTION1:
            put all tha values in a tuple and return it.
            CODE:
            def fn():
            i =10
            j = i+10
            k = (i,j)
            return k
            l = fn()
            print l
            OUTPUT:
            (10,20)

            Then separate the elements simply as if for any other tuple.

            SOULTION2:
            declare variables you want out of the function as global.

            CODE:
            def fn():
            global i
            i =10
            j = i+10
            return j
            k = fn()
            print i
            print k

            Here, i is global, that is, as if it is 'defined' outside of any fn in the module. It can be accessed from any fn in same module (and also on different module using

            CODE:
            from module import *

            Think that should solve your problem.

            Bye.

            Comment

            • jlm699
              Contributor
              • Jul 2007
              • 314

              #7
              Originally posted by AIProgrammer
              Hi,
              Your problem has two solutions...
              [redacted]
              Please use code tags in your posts. Refer to the Posting Guidelines about how to properly respond to questions.

              Comment

              Working...