-4

Basically I have a application that sends a value to another function(located in another file) and I want other functions(within that file) to have access(if it matters, its read access) to that variable.

For example:

def function1(pricelist):
     #do something

.... other functions

def fucntion200():
    #Does something else but needs to refer to that pricelist that was provided to function1

I tried doing a pricelist = pricelist in function1 so maybe if I declare it in the file it'll be avail, but that didn't work and when I make it global I get an error saying its local and global.

I have read: Using global variables in a function other than the one that created them and don't think it fully applies(or I have so far been unable to).

Any ideas?

Thanks!

Community
  • 1
  • 1
Lostsoul
  • 25,013
  • 48
  • 144
  • 239
  • 2
    just return `pricelist` from function1. – RanRag Feb 02 '12 at 22:45
  • 1
    There is a severe lack of code being presented here – jdi Feb 02 '12 at 22:47
  • but they are not actually feeding into each other. function1 happens then a bunch of other functions then it gets to function2. I can keep passing them down the chain but I thought there would be a better way. – Lostsoul Feb 02 '12 at 22:48
  • @jdi..the file is big and I don't think its relevant to a general question. – Lostsoul Feb 02 '12 at 22:48
  • 1
    @Lostsoul - having at least more understanding of what you are trying to achieve IS relevant. Most likely there is a design issue here. Maybe you need to use class structures. Maybe you do need a global constant in one of your modules. Only you know the context. – jdi Feb 02 '12 at 22:50
  • @jdi understood. I'm just trying to hack some code together, I guess classes could fix the problem because the variable would be shared, but is this impossible using functions only? – Lostsoul Feb 02 '12 at 22:53
  • I think the problem here is that you are in fact trying to hack something together, so there really isn't enough context for anyone else to give you a solid answer. If you are trying to avoid doing object oriented programming, then with functions, you just need to pass around objects or modify them. – jdi Feb 02 '12 at 23:01
  • 1
    okay thats what I needed to know I guess. Your right, I am not that good with object oriented programming so I was sticking to what I was more confortable with(functions). I guess I'll bite the bullet and use objects, I guess it'll be a good time to learn. Thanks for your answer and patience in explaining. – Lostsoul Feb 02 '12 at 23:07

3 Answers3

1

It really depends on the control flow of your application. What is calling function1 and is it the one that ends up calling function200? At the very basic level, your function200 would take a pricelist arg as well, and modify it in place:

def function200(pricelist):
    pricelist[0] = 50

It would be most desirable to have one module using the other as a utility to where it can call the functions with the params it needs. Whatever is calling function1 could theoretically have access to function200 and call it as well.

Using module globals should probably be reserved for constant values or things that aren't going to be modified by a bunch of unknown sources, but it would be something like:

moduleA.py

constList = ['foo']

moduleB.py

import moduleA
print constVal
# ['foo']
constVal.append('bar')

But if all you are doing is writing top level functions, then they should all take parameters to operate on, and optionally return new versions or modify in place.

jdi
  • 90,542
  • 19
  • 167
  • 203
  • I understand what your saying and I'm sorry, I think I didn't provide some details(I didn't realize they were relevant). I can't import moduleA into moduleB because I'm already importing moduleB into moduleA and it get errors when I do a circular import. typically this is what I do, and then refer to the variable as moduleA.constList Also, function200 is not being called directly from function1, function1 kind of sends to other functions based on results within its own function, and so on until a function(could be several functions later) sends to function2000 – Lostsoul Feb 02 '12 at 23:05
  • You probably just need to use classes and rethink the organization of your program. There is always an entry point, and something is using something else. Even if there are 200 pieces of logic between those two function calls, it would eventually unwind back up to the top of the stack where pricelist exists. Sorry we couldn't give you more of a concrete solution. – jdi Feb 02 '12 at 23:12
  • Thank you, I think you gave me a great solution. I learned programming in highschool(very basic) almost 2 decades ago and I didn't learn OO very well so I just avoided using it. This problem showed me a reason to use it and learn. Codes getting unnecessary complicated and messy, so I'll just take it as a chance to learn using classes. Thank you for setting me on the right direction. – Lostsoul Feb 02 '12 at 23:14
1

pass the pricelist to the first function, let it modify/use it any way it needs and return the modified pricelist. send this new modified pricelist to the second function.

def function1(pricelist):
     #do something
     #modify pricelist somehow
     pricelist[0] = 1
     return pricelist


#pass the new modified pricelist to other function
def function200(pricelist):
    #do anything you need



#in your main() :
mypricelist = [1,2,3]
mypricelist = function1(mypricelist)
function200(mypricelist)
jb.
  • 9,921
  • 12
  • 54
  • 90
1

Basically I have a application that sends a value to another function...

this a curios sentence because you cannot "send" values to a function, not to a regular one. You can send values to a coroutine which is something different from a function.

def grep(pattern):
    print 'Looking for %s' % pattern
    while True:
        line = (yield)
        if pattern in line:
            print line,

g = grep('python')
g.next()
g.send('python generators rock!')

After this little excursus about python generators maybe a decorator can do what you need but I don't understand exactly what you need.

>>> def func(pricelist):
...     print(pricelist)
... 
>>> def func2(f):
...     def wrapper(pricelist):
...         pricelist.append('cucu')
...         return f(pricelist)
...     return wrapper
... 
>>> func([1, 2, 3])
[1, 2, 3]
>>> func = func2(func)
>>> func([1, 2, 3])
[1, 2, 3, 'cucu']
>>>

But give us more information on what you need.

mg.
  • 7,822
  • 1
  • 26
  • 30
  • It's perfectly reasonable to call the passing of values to a function when it's called "sending" them, IMO. – Wooble Feb 03 '12 at 00:30
  • @Wooble, I read more carefully the question and the term is good in this context and my answer become less coherent. – mg. Feb 03 '12 at 09:43