473,397 Members | 1,974 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,397 software developers and data experts.

dynamically creating variables

I am writing a parser and I have a routine that can digest
a series of tokes:

def digest(*args)

I use it like this: digest("FOR", "IDENT", "EQ", "INTVAL", "COLON").
This part works fine. My digest function returns a list of the values
associated with each token.

dummy1, ident, dummy2, start = digest("FOR", "IDENT", "EQ", "INTVAL",
"COLON")

But I dont like the way this looks and I want my digest routine to do
more work. I want to be able to specify the variable bindings in the
call to digest - and not have them be on the LHS of the assignment.

Ideally I want to call digest like this:

digest("FOR", ("IDENT",ident), "EQ", ("INTVAL",start), "COLON")

and have ident and start be local variable defined in the place that
calls digest.

Is it possible to do something like that?

-Ram

Mar 3 '06 #1
3 1719
> Is it possible to do something like that?

No, you can't change the binding of variables in your calling space.

Mar 3 '06 #2
ra*****@gmail.com wrote:
I am writing a parser and I have a routine that can digest
a series of tokes:

def digest(*args)

I use it like this: digest("FOR", "IDENT", "EQ", "INTVAL", "COLON").
This part works fine. My digest function returns a list of the values
associated with each token.

dummy1, ident, dummy2, start = digest("FOR", "IDENT", "EQ", "INTVAL",
"COLON")

But I dont like the way this looks and I want my digest routine to do
more work. I want to be able to specify the variable bindings in the
call to digest - and not have them be on the LHS of the assignment.

Ideally I want to call digest like this:

digest("FOR", ("IDENT",ident), "EQ", ("INTVAL",start), "COLON")

and have ident and start be local variable defined in the place that
calls digest.

Is it possible to do something like that?

-Ram

Can't do what you want specifically, but you can stuff everything into
a dictionary and pass it around.

I'm not 100% sure I understand what you want to accomplish, but I
hope this helps. Have digest return a dictionary of tokens/values.

tokenDict=digest("FOR", "IDENT", "EQ", "INTVAL", "COLON")

then refer to things as tokenDict['ident'], tokenDict['start']

-Larry Bates
Mar 3 '06 #3
Crutcher wrote:
Is it possible to do something like that?

No, you can't change the binding of variables in your calling space.


Unless your calling space is global

py> def doit():
.... globals()['wilma'] = 'pebbles'
....
py> doit()
py> wilma
'pebbles'

But that would be crazy.
Mar 3 '06 #4

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

Similar topics

4
by: Eric | last post by:
How can I dynamically assign an event to an element? I have tried : (myelement is a text input) document.getElementById('myelement').onKeyUp = "myfnc(param1,param2,param3)"; ...
8
by: Falc2199 | last post by:
Hi, Does anyone know how to make this work? var sectionId = 5; repeat_section_sectionId(); function repeat_section_5(){ alert("firing"); }
7
by: pmclinn | last post by:
I was wondering if it is possible to dynamically create a structure. Something like this: public sub main sql = "Select Col1, Col2 from Table a" dim al as new arraylist al =...
6
by: TB | last post by:
Hi All: I have this page where a rows / cells are programmatically added to to table by pushing a button. The rows contain a textbox and a associated button. What I want to is to be able to...
5
by: Chris Lieb | last post by:
I am trying to add an event listener to the keyup event for some text inputs that I am creating dynamically. I have no problems getting it to work in Firefox, but IE just ignores them. I have...
2
by: ann | last post by:
Is there any way to dynamically creat variables? In other words, I want to be able to do something like that: for(int i=0; i<size; i++){ string temp+i = i*3; }
9
by: netasp | last post by:
hi all, how can I populate one aspx form when page is loading based on page ID? for example: loading page A (to search for VB code) would display labels and texboxes, dropdown lists all related...
2
by: Mitesh | last post by:
I have an HTML page with references to PHP variables. This HTML is included from another PHP page. How can I save the HTML page dynamically i.e. when all the PHP variables are written into the HTML...
8
by: =?Utf-8?B?U2hhd24=?= | last post by:
Hi; i just started research reflection and i'm wondering if i have an empty class file can i use reflection to add member variables and attributes dynamically and then instantiate the class? What...
1
by: semomaniz | last post by:
I have a form where i have created the form dynamically. First i manually added a panel control to the web page. Then i added another panel dynamically and inside this panel i created tables. I have...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.