Deformed Form

Stephen Hansen me+list/python at ixokai.io
Thu Jun 10 13:29:22 EDT 2010


On 6/10/10 10:11 AM, Victor Subervi wrote:
> On Thu, Jun 10, 2010 at 10:30 AM, Stephen Hansen (L/P) <me+list/
> python at ixokai.io> wrote:
>>
>> But what does "cannot be called" mean? "Cannot" usually means "an error
>> happened" -- in which case you shouldn't really even mention it unless
>> you're gonna back it up with a traceback.
>>
> 
> That is correct. It throws an error if I try and access the variable from
> the main script, because I try and use the value of that variable and for
> some reason I can't retrieve it.

I think you don't fully understand namespaces at the moment.

Every file is a module; think of it like a big box with a name on it.
Inside that box there are tags which have names written on them,
attached to some smaller boxes.

The variable in question, "new_passengers_curr_customers", is one such
tag. It is connected to a box, which is an object or value of some sort.

Let's assume you have approximately this code in a certain file, which
we shall name "creating.py" for example purposes:

  def create_edit_passengers3():
    new_passengers_curr_customers = ...

You have to picture this as a series of boxes, one within another. Once
this module is imported, and everything's loaded, your namespace looks
vaguely like this (this is a demonstration only, not a real thing):

{"creating":
    {"create_edit_passengers3":
        {"new_passengers_curr_customers": ...}
    }
}

I drew that out as nested dictionaries just to try to illustrate what's
going on.

new_passengers_curr_customers is a name that is *local* to the function.
It exists solely inside that function. If you want to use it anywhere
"else", you have to either a) call the else, passing it, b) if the else
called this function, then simply return it, or b) in some other
*explicit* way set or store the variable in a place that is accessible
to else.

I'm not sure what your "else" is here to tell you which is best, a, b,
or c. If you want this variable to be accessible to the "main script"
(its not clear to me exactly which is 'main' in your scenario), which in
my above example is the module named 'creating', then a global variable
may work for you (I'm not going to harp on the danger of globals to you,
you're not to the skill set of object oriented stuff yet), as in:

  new_passengers_curr_customers = None

  def create_edit_passengers3():
    global new_passengers_curr_customers

    new_passengers_curr_customers = ...

What this chunk of code does is establish, in the top-level of your
module, a new variable. Inside your function, you then explicitly state,
"When I assign to new_passengers_curr_customers, I want to assign to the
existing module-level variable, and NOT create a new local with the same
name." That's what the global statement does: but note, 'global' really
means /module level/, and not /universal/ -- it won't be accessible in
another module.

If you have certain variables you need to access in more then one
module/file, what you can do is create a separate module that both
import separately, and share.

As in, a file called "state.py", which would say like:

  new_passengers_curr_customers = None

Then in your creating.py:
  import state

  def create_edit_passengers3():
    state.new_passengers_curr_customers = ...

Then anywhere you want to access that variable, you do 'state.blah'
instead of 'blah'.

Note: I'm entirely unsure if its wise to use -either- of these methods
in the context of a web application, unless this application is
single-threaded, and this state information is entirely temporary:
meaning that when you are done with it, and before a new request hits,
you are sure to clear it out so you don't get two requests corrupting
each-other.

-- 

   Stephen Hansen
   ... me+list/python (AT) ixokai (DOT) io

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 553 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20100610/70458236/attachment-0001.sig>


More information about the Python-list mailing list