473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How can I access other scopes (NOT global)

I was hoping that someone on the group might have an idea of how to
access scopes (or symbol tables) other than the one you are currently
running in. (and no I don't mean global)

In particular, I'm trying to use register_tick_function, and have the
function that it calls be able to get at variables in the scope of
what (otherwise) would currently be executing. Unfortunately the scope
of the function called by register_tick_function seems to be it's
own.

An example in code:
<?php

function a(){
$my_a = "mystery_value";
b();
}

function b(){
echo($my_a); //doesn't work
}

a();
?>

So I'm trying to figure out how to get at $my_a, none of the obvious
(to me) solutions are viable, I can't simply pass $my_a to b because b
is being called by a tick. I can't store $my_a in the globals because
I don't get to modify any of the code in a(). (I would be more than
happy with a solution where all scope variables were pushed into
global, but I can't think of a way to do this.
I realize that what I'm trying to do runs against the grain of what is
supposed to be legal in a language. (the whole point of scopes is to
protect you from whats in your parents) but I'm really hoping there is
a solution anyway. Ideally I would like to do this without loading the
ADB module, but if there is something in there that will help, I'm
open to that idea as well.

Jan 7 '08 #1
3 1410
On Mon, 07 Jan 2008 18:47:51 +0100, giblfiz <gi*****@gmail.comwrote:
I was hoping that someone on the group might have an idea of how to
access scopes (or symbol tables) other than the one you are currently
running in. (and no I don't mean global)
You can't, you either have to pass the variable, or a reference to it.
--
Rik Wasmus
Jan 7 '08 #2
..oO(giblfiz)
>I was hoping that someone on the group might have an idea of how to
access scopes (or symbol tables) other than the one you are currently
running in. (and no I don't mean global)

In particular, I'm trying to use register_tick_function, and have the
function that it calls be able to get at variables in the scope of
what (otherwise) would currently be executing. Unfortunately the scope
of the function called by register_tick_function seems to be it's
own.
Correct. That's how it works in almost every language.
>An example in code:
<?php

function a(){
$my_a = "mystery_value";
b();
}

function b(){
echo($my_a); //doesn't work
}

a();
?>

So I'm trying to figure out how to get at $my_a, none of the obvious
(to me) solutions are viable, I can't simply pass $my_a to b because b
is being called by a tick. I can't store $my_a in the globals because
I don't get to modify any of the code in a(). (I would be more than
happy with a solution where all scope variables were pushed into
global, but I can't think of a way to do this.
What about OOP? Putting a() and b() into a class would give you kind of
a common namespace for both.

And what about a() - are you not able to modify it or do you just don't
want to modify it? If the code of a() is "fixed", how is this entire
thing supposed to work? Without some ugly hacking it would be impossible
in almost every language. Maybe you can post some more details about
what this whole thing is about and what you're trying to accomplish.

Micha
Jan 7 '08 #3
I'm trying to write an "introspective debugger" file. (it's tempting
to use the word class, but it's just not true.) The idea being that
you include the file at the beginning of your all ready written code,
add a few STOP statements, and then visit you script as normal. Only
now you get a simple ajax debugger which lets you view source files,
your stack, step through the code, resume operation (to next STOP),
and watch variables.

Sounds impossible? Sounds insane? Using some of the less well known
PHP functions, and some tricks with sockets I have figured out how to
do all of this except for watching variables that are not globals.
(take a look at register_tick_function, and at debug_backtrace and
then think about it for a few seconds, most of what I'm talking about
is actually in there)

So in answer to your question, no I CAN'T modify a(), because a() is
actually pretty much every function in the program. (since b() is
being called on every tick, from every function and every method) I
should have realized that by putting that a() b() example into place
people were going to assume that it was actually my situation. My
actual situation is:
<?php
register_tick_function("ticker");

ticker(){
$my_var_in_ticker_scope = "known value";
var_dump(get_defined_vars());
var_dump(debug_backtrace());
}

declare(ticks=1);

a(){
$my_a = "mystery value";
1+1;
}

a();

?>

So there is a point, right about when 1+1 is being run, when ticker
will be called and its results will be:
A dump of all the variables in the global scope, and
$my_var_in_ticker_scope, and a dump of the stack which will more or
less say:

2) Ticker()
1) a()

My best bet before was trying
register_tick_function(Ticker,get_defined_vars()), but this evaluates
get_defined_vars() once, when register_tick_function is called, not
every time ticker gets called. (this behavior seems obvious to me in
retrospect, but maybe someone knows how force it to be re-evaluated on
a run by run basis. nothing I know of in the language can do this)

Right now I have three "best bets":
1) Possibly throwing a user defined error, and then seeing what scopes
can be reached as part of a user defined error handler. (I haven't
played with this yet, but it strikes me as a longshot)
2) A similar idea using throw and catch. I am even less enthusiastic
about this idea because it pulls you down out of your current scope in
an unrecoverable way. But if there is a way to register a catch block
that is universal, that would work.
3) Perhaps there is something really interesting that can be done
using eval. I haven't really thought about it too deeply yet.

I'm also open to any other ideas, if anyone has any.
Jan 7 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Fernando Rodriguez | last post by:
Hi, I have a parameter defined in a module, called PREVIEW. Many functions use it's value to modify their behavior. A function called dispatch checks the user arguments in sys.argv and calls...
3
by: Nils Grimsmo | last post by:
hi, i'm having some trouble nesting functions. consider the following: def h(): x = 1 def g(): print x # ok, x is taken from h g()
6
by: Noah | last post by:
I thought that Python has a builtin dictionary that associates global variable names with their values. I have forgotten how to do this and I can't seem to come up with the right search keywords to...
5
by: Dave Benjamin | last post by:
I ran into an odd little edge case while experimenting with functions that create classes on the fly (don't ask me why): >>> def f(x): ... class C(object): ... x = x ... print C.x ......
4
by: kj | last post by:
I'm a Perlhead (there, I said it). Years ago I made a genuine attempt to learn Python, but my intense disappointed with the way Python deals with scopes ultimately sapped my enthusiasm. I...
15
by: __PPS__ | last post by:
Hello everybody, in my exam in c++ I answered that destructors may be declared private, public or protected. The question was: "What possible scopes (public or private) can a destructor have?" ...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
37
by: Tim N. van der Leeuw | last post by:
Hi, The following might be documented somewhere, but it hit me unexpectedly and I couldn't exactly find this in the manual either. Problem is, that I cannot use augmented assignment operators...
9
by: Pyenos | last post by:
#############################CODE############################## t_len=0 class WORK: def getwork(self): def formattable(table_to_process,type): TYPE= #list of types to format if type==TYPE: def...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.