[Tutor] Global Variables

Luke Paireepinart rabidpoobear at gmail.com
Fri Nov 20 22:52:18 CET 2009


On Fri, Nov 20, 2009 at 2:51 PM, Joseph Fennell <josephfennellster at gmail.com
> wrote:

> Quick introduction,  I'm Joseph Fennell, and I've been playing with python
> for the past 6 years (primarily in the interpreter) and have finally gotten
> started on a full project and I've run into a small problem.  I have defined
> some modules, and when I import them into the page I want to use them on
> after I run the program it tells me the variable I'm trying to pass through
> them is not defined.
>
> I assume that it's because the variable is considered local and the
> imported functions are not technically local to that document.
>
> The code is to pass the contents of a database through to filter by name
> and quantity and as follows
>

The example code you provided is very confusing.

>
> =================================================
>
> cHandler.execute("SELECT * FROM table")
>
> #sets results from above to nl
>
> nl = cHandler.fetchall()
>
> dn = raw_input("User input: ")
>
> nl2 = []
>
> dname()
> ########
> def dname():
>     global nl
>     for ndc in nl:
>         if ndc[1] == dn:
>             nl2.append(ndc,)
>         else:
>             continue
> #########
> tuple(nl2) = nl2
>
> qtysrch()
> #########
> def qtysrch():
>     global nl2
>     for ndc in nl2:
>         if ndc[7] == 0:
>             continue
>         else:
>             print ndc
>

Are you saying these are in separate files?

Please repost the code in a more logical fashion.  Eg.

-------------------------------
~~~ somemodule.py
-------------------------------
def foobar():
    print "foobar!"

-------------------------------
~~~ main.py
-------------------------------

import somemodule
somemodule.foobar()
-------------------------------


This will make it much easier to understand your question, because I don't
see how the code sample you provided applies to your question whatsoever.
Also, if you have a lot of code post it on pastebin (I wouldn't consider
this a lot of code).  Just for future reference.
-Luke
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091120/c0933984/attachment-0001.htm>


More information about the Tutor mailing list