changing a var by reference of a list

J

Jorgen Bodde

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
 
L

Larry Bates

Jorgen said:
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
 
D

Diez B. Roggisch

Jorgen said:
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
 
J

Jorgen Bodde

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

Jorgen said:
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
 
C

Carsten Haese

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,
 
D

Duncan Booth

Jorgen Bodde said:
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.
 
B

Bruno Desthuilliers

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.
 
J

Jorgen Bodde

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top