0

For most of you this is probably common sense but I am finding it very, very hard to get a handle on this. If you create a function such as

def one():
    x=1
    y=2
    z=3
    h=33
    j=454
    l=353
    m=898

then lets say I have another function, def two():, and I want to use the variables in def one() into def two, and I also create two new variables in def two(). How would I go about doing that.

Then, lets say that I have a third function def three(), how would I use the variables in def 1 and def 2 into def 3? The methods I am using are very very inefficient and probably doing more than is required. What I do is in def two, a add all the variables from def one into def two's parameters, and then I also call function one in the def of function 2. I know there are numerous threads on this on SO, and I have looked through many of them and they don't simply it. They use very specific and advanced code that I am unfamiliar with and therefore don't understand. My example is very straightforward and simple and I'm sure if answered it will help many newcomers.Thanks!

EDIT:I don't want a "simple" way that ends up using advanced code I am not familiar with. I want an "efficient" method that is simple to understand.

SeesSound
  • 503
  • 4
  • 13
  • 24
  • 1
    Maybe you should wrap it in a class and use `self`. check http://stackoverflow.com/q/6236032/1031900 – Ofir Farchy Nov 01 '12 at 07:11
  • A class sounds like the best fit. You might want to go through the python tutorial before you get in too deep though. – monkut Nov 01 '12 at 07:20
  • I do not comprehend your implied concepts of "simple", "advanced" or "efficient". I suspect they are misguided. I voted to close this because I think it is overly broad, and really it is not even Python-specific anyway. – Karl Knechtel Nov 01 '12 at 08:07
  • @KarlKnechtel thanks I will remember this in the future. I would've appreciated it if you waited a bit until after I got a good answer before closing it. – SeesSound Nov 02 '12 at 07:53

5 Answers5

3

I think the best solution would be adopting an OO paradigm for your code (classes, methods and inheritance).

Using simple functions, you can just choose between using global variables and local scoped.

2

Data is passed into functions as arguments, and out of functions as return values. The ideal case for a function is when you have no need to access variables out of the scope of the function.

If you have a lot of numbers, perhaps you should declare them as a list (easier to manage) and then pass them into functions as arguments and out of functions, as the transmuted results, as a return value?

Andrew Gorcester
  • 19,595
  • 7
  • 57
  • 73
  • if not using a list, then if I use a return in the definition of function one, then I call the function one in the definition of function2, will all the variables values now be a part of the definition of function2? – SeesSound Nov 01 '12 at 07:14
  • @user1716168 Nothing will magically become "a part of the definition of function2"; the point of calling a function that returns a value is to use it in some expression, e.g. variable assignment: `val = one(param1, param2)`. Now `val` holds whatever `one` returned. All that can be inside another function. – Lev Levitsky Nov 01 '12 at 07:18
1

You can use the global keyword to create and modify global variables like this:

In [1]: def one():
   ...:     global a
   ...:     a = 1
   ...: 

In [2]: one()

In [3]: a
Out[3]: 1

However, having that many (or maybe even any) global variables is not considered a good practice. You can declare one global list and keep all those values in it, that would be cleaner; although you should try your best to stick with using function arguments and return values as outlined in Andrew's answer.

Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
0

Without being at all clear on what you're trying to do, here's a simple way you can do this:

class One(object):
    x=1
    y=2
    z=3
    h=33
    j=454
    l=353
    m=898


class Two(One):
    n=42
    o=1337

print Two.x, Two.n  # 1, 42
Eric
  • 95,302
  • 53
  • 242
  • 374
0

If I understand what do you need then I think simplest way that python brings you is next ->

class Helper:pass 

# use name space
def one(a):  
    a.x=1
    a.y=2
    a.z=3
    a.h=33
    a.j=454
    a.l=353
    a.m=898

def two(a):
    a.y=7

a=Helper()

one(a)
print(a.x,a.y)
two(a)
print(a.x,a.y)

#-------------------------------------------------------------------
# I think you have solution and dont need to read next lines :)
# next lines are just bonus ;)
#-------------------------------------------------------------------

# you could use more independent "namespaces"
b=Helper()
one(b)
print(b.y)
print(a.y)  # "namespace a" is unchanged

# if you like to see which attributes are defined you could use funciton dir
# (under python3 it is a little more complicated)
print(dir(a))
print(dir(b))

# please dont do next lines at home!!! :P:P:P (__builtins__ is special you could get unwanted results!) 
one(__builtins__)

print(x) # 
Liso
  • 2,165
  • 1
  • 16
  • 19