indirect references

Emile van Sebille emile at fenx.com
Tue Jun 6 08:20:08 EDT 2000


Well, that calls for a response! ;-)

>>> def spam(a):
  import sys
  try:
    1/0
  except ZeroDivisionError:
    callers_globals =
sys.exc_info()[2].tb_frame.f_back.f_globals
    callers_locals =
sys.exc_info()[2].tb_frame.f_back.f_locals
  if id(callers_locals) == id(callers_globals):
    callers_globals = {}
  retval = []
  for ns in (callers_locals, callers_globals):
    for calling_var_name, calling_var_value in ns.items():
      if id(calling_var_value) == id(a):
        retval.append((ns, calling_var_name))
  for ns, var in retval:
    ns[var]="spam and "+ns[var]

>>> a = "eggs"
>>> spam(a)
>>> a
'spam and eggs'
>>> a = "eggs"
>>> b = a
>>> spam(a)
>>> b
'spam and eggs'
>>>

Of course, it's not not going to work except
in console mode.

Emile van Sebille
emile at fenx.com
-------------------


----- Original Message -----
From: Ken Seehof <12klat at sightreader.com>
Newsgroups: comp.lang.python
To: <python-list at python.org>
Sent: Tuesday, June 06, 2000 5:17 AM
Subject: Re: indirect references


> I'm not sure exactly what the effect is that you are
after, so I assume
> that what you are looking is something like call by
reference in a
> fuction, e.g.
>
> >>> a = "eggs"
> >>> spam(a)
> >>> print a
> spam and eggs
>
> Unfortunately the margin is too small to contain my
implementation.  :-)
>
> shaleh at debian.org wrote:
>
> > How do I do this in python:
> >
> > foo = 'string'
> > bar = foo
> >
> > somehow assign to bar and change the value of foo.
> >
> > Sent via Deja.com http://www.deja.com/
> > Before you buy.
>





More information about the Python-list mailing list