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

changing a var by reference of a list

Hi,

I am trying to simplify my code, and want to automate the assigning of
variables I get back from a set. I was thinking of putting the
variables I want changed in a list:

L = [self._varA, self._varB ]

self._varA is a variable I want to change when I pass L to a function.
I know doing this;

L[0] = 12

Will replace the entry self._varA with 12, but is there a way to
indirectly change the value of self._varA, through the list, something
like a by-reference in C++ or a pointer-pointer?

With regards,
- Jorgen
May 8 '07 #1
7 1342
Jorgen Bodde wrote:
Hi,

I am trying to simplify my code, and want to automate the assigning of
variables I get back from a set. I was thinking of putting the
variables I want changed in a list:

L = [self._varA, self._varB ]

self._varA is a variable I want to change when I pass L to a function.
I know doing this;

L[0] = 12

Will replace the entry self._varA with 12, but is there a way to
indirectly change the value of self._varA, through the list, something
like a by-reference in C++ or a pointer-pointer?

With regards,
- Jorgen
You can make self._varA and self._varB instances of a class and
assign a value. Not tested.

class _var:
pass

self._varA=_var()
self._varB=_var()
L=[self._varA, self._varB]

then in function (or method of a class instance):

L[0].value=12

Another way is to use a class to pass everything:

class _vars:
def __init__(self, vars=None):
if vars is not None:
for varname, value in vars.items():
setattr(self, varname, value)
return
vars=_vars({'_varA': 0, '_varB': 0})

Then you can access:

vars._varA
vars._varB

-Larry

l
May 8 '07 #2
Jorgen Bodde wrote:
Hi,

I am trying to simplify my code, and want to automate the assigning of
variables I get back from a set. I was thinking of putting the
variables I want changed in a list:

L = [self._varA, self._varB ]

self._varA is a variable I want to change when I pass L to a function.
I know doing this;

L[0] = 12

Will replace the entry self._varA with 12, but is there a way to
indirectly change the value of self._varA, through the list, something
like a by-reference in C++ or a pointer-pointer?
No, there isn't.

But you could do

L = ['_varA']

for v in L:
setattr(self, v, value)

Diez
May 8 '07 #3
Ok thanks,

I will try this approach. The idea was that I could give a list to the
SQL execute command, so that the results coming back would
automatically be assigned to variables.

With regards,
- Jorgen

On 5/8/07, Diez B. Roggisch <de***@nospam.web.dewrote:
Jorgen Bodde wrote:
Hi,

I am trying to simplify my code, and want to automate the assigning of
variables I get back from a set. I was thinking of putting the
variables I want changed in a list:

L = [self._varA, self._varB ]

self._varA is a variable I want to change when I pass L to a function.
I know doing this;

L[0] = 12

Will replace the entry self._varA with 12, but is there a way to
indirectly change the value of self._varA, through the list, something
like a by-reference in C++ or a pointer-pointer?

No, there isn't.

But you could do

L = ['_varA']

for v in L:
setattr(self, v, value)

Diez
--
http://mail.python.org/mailman/listinfo/python-list
May 8 '07 #4
On Tue, 2007-05-08 at 15:40 +0200, Jorgen Bodde wrote:
Ok thanks,

I will try this approach. The idea was that I could give a list to the
SQL execute command, so that the results coming back would
automatically be assigned to variables.
It's not possible to do that exactly as stated. A function can not
modify its caller's namespace. (There is probably some dirty trick that
can do this anyway, but you don't want to use dirty tricks.) Also, I'm
not sure I'd want a function to pollute my local namespace with any
variables of its choice. How would I know that it won't overwrite any
variable whose contents I need? (Don't try to answer, this is a
rhetorical question!)

Most DB-API implementations have a facility to return query results as
dictionaries or "a bag full of attributes"-type objects. The syntax for
achieving this varies between implementations, because this is a
non-standard addition outside the DB-API spec. With InformixDB for
example, it would look something like this:

import informixdb
conn = informixdb.connect("stores_demo")
cur = conn.cursor(rowformat=informixdb.ROW_AS_OBJECT)
cur.execute("select * from customer")
for row in cur:
print row.customer_num, row.company

This allows accessing the result columns as attributes of a row object,
which is 99% as convenient as having local variables assigned to the
results, and its 15000% cleaner.

I strongly suggest you find an equivalent mechanism to use with whatever
database module you're using, since it's a fairly safe bet that you're
not using Informix. We could help you find that mechanism if you told us
what database and what DB-API module you're using.

Best regards,

--
Carsten Haese
http://informixdb.sourceforge.net
May 8 '07 #5
"Jorgen Bodde" <jo*************@gmail.comwrote:
I will try this approach. The idea was that I could give a list to the
SQL execute command, so that the results coming back would
automatically be assigned to variables.
Don't forget that in Python a function can return multiple results (or
rather can return a sequence). That means you can do something like:

varA, varB = doSomething()

where doSomething would execute your SQL and return two results.
May 8 '07 #6
Jorgen Bodde a écrit :
Ok thanks,

I will try this approach. The idea was that I could give a list to the
SQL execute command, so that the results coming back would
automatically be assigned to variables.
You may want to have a look at SQLAlchemy.
May 8 '07 #7
Hi Bruno,

Unfortunately SQLAlchemy will be too involved at this point I will
have to rewrite a lot of code to remove my current DB solution and use
that. Howerver I've learned from my mistake and the next project will
use it, as it seems to be a nice way of mapping objects to databases..

I'v solved it by just sending back the record set from sqlite3 because
I noticed sometimes a 1:1 mapping cannot be done from column value to
variable anyway..

Thanks everyone,
- Jorgen
May 9 '07 #8

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

Similar topics

15
by: oom | last post by:
I am a bit of a newbie when it comes to python, when working with lists today I noticed some very odd behaviour, any suggestions welcome: Python 2.2.3 (#1, Nov 6 2003, 14:12:38) on linux2...
2
by: pablo | last post by:
Dear NGers, I want to keep some images just left of some DIVs. But a change in text size leaves my images at the original position. Which event is generated by changing the text size from the...
8
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
24
by: Charles Crume | last post by:
Hello; My "index.htm" page has 3 frames (content, navigation bar, and logo). I set the "SRC" of the "logo" frame to a blank gif image and then want to change it's contents after the other two...
10
by: Daniel | last post by:
how to make two references to one string that stay refered to the same string reguardless of the changing value in the string?
22
by: Zytan | last post by:
I have public methods in a form. The main form calls them, to update that form's display. This form is like a real-time view of data that is changing. But, the form may not exist (it is...
5
by: marton | last post by:
Hi there, I'm new to posting on the forum, and I've been working with MS Access for a couple of years, but and other than that know nothing about programming. I have a fairly simple, networked,...
1
by: DrJarmin | last post by:
Hello The problem is this: in the criteria for a list box I reference the parent form - and Access KEEPS changing the criteria for one that won't work. Details below: I have a couple of list...
3
by: Markus Dehmann | last post by:
Hi, I observed a behavior that I didn't expect: I have a vector of sets. I iterate over the first of these sets, and while I iterate over it, I add another set at the end of the vector. I...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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?

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.