Binding a variable?

P

Paul Dale

Hi everyone,

Is it possible to bind a list member or variable to a variable such that

temp = 5

list = [ temp ]

temp == 6

list

would show

list = [ 6 ]

Thanks in advance?

Paul
 
B

bonono

I think not. temp is a name, not a variable. I believe temp=5 means it
points to an immutable object 5. temp=6 means now it points to another
immutable object 6. list=[temp] would resolve to whatever object temp
is pointed to at that moment.

You can try temp=[1].
 
K

Kent Johnson

Paul said:
Hi everyone,

Is it possible to bind a list member or variable to a variable such that

No, Python variables don't work that way.

The name 'temp' is now bound to the integer 5. Think of temp as a pointer to an integer object with value 5.
list = [ temp ]

the name 'list' is bound to a list whose only member is a reference to the integer 5
temp == 6

Ok now temp is bound to a new integer, but the list hasn't changed - it's member is still a reference to 5
list

would show

list = [ 6 ]

You have to put a mutable object into the list - something whose state you can change. For example this works, because temp and lst[0] are references to the same mutable (changeable) list:
>>> temp = [6]
>>> lst = [temp]
>>> lst [[6]]
>>> temp[0] = 5
>>> lst
[[5]]

Kent
 
M

Mike Meyer

Paul Dale said:
Hi everyone,

Is it possible to bind a list member or variable to a variable such that

temp = 5

list = [ temp ]

temp == 6

list

would show

list = [ 6 ]

No. You need to either put a mutable in the list, or subclass list so
that indexing gets the value, looks it up in the appropriate
namespace, and returns that value.

<mike
 
D

David Wahler

Paul said:
Hi everyone,

Is it possible to bind a list member or variable to a variable such that

temp = 5

list = [ temp ]

temp == 6

list

would show

list = [ 6 ]

Thanks in advance?

Paul

Python doesn't have "variables" -- a statement like "temp = 5" just
binds the name "temp" to an int object with value 5. When you say "list
= [temp]", that's putting the int object into the list; the name "temp"
is just a label for it, not a variable or container of any kind. Since
int objects are immutable, you can only change the value by assigning a
new object in its place.

That said, you can accomplish something like this by creating a new
class:

-----------------------------------
class MyInt:
pass
temp = MyInt()
temp.i = 5
list = [temp]
temp.i = 6
print list[0].i
6
-----------------------------------

Once your object is mutable, any changes to it will be reflected
anywhere there is a reference to it.

-- David
 
T

Tom Anderson

Is it possible to bind a list member or variable to a variable such that

temp = 5
list = [ temp ]
temp == 6
list

would show

list = [ 6 ]

As you know by now, no. Like any problem in programming, this can be
solved with a layer of abstraction: you need an object which behaves a bit
like a variable, so that you can have multiple references to it. The
simplest solution is to use a single-element list:
temp = [None] # set up the list
temp[0] = 5
list = [temp]
temp[0] = 6
list
[[6]]

I think this is a bit ugly - the point of a list is to hold a sequence of
things, so doing this strikes me as a bit of an abuse.

An alternative would be a class:

class var:
def __init__(self, value=None):
self.value = value
def __str__(self): # not necessary, but handy
return said:
temp = var()
temp.value = 5
list = [temp]
temp.value = 6
list
[<<6>>]

This is more heavyweight, in terms of both code and execution resources,
but it makes your intent clearer, which might make it worthwhile.

tom
 
S

Steven D'Aprano

Paul Dale said:
Hi everyone,

Is it possible to bind a list member or variable to a variable such that

temp = 5

list = [ temp ]

Don't use the names of built-in functions as variables.
temp == 6

list

would show

list = [ 6 ]

No. You need to either put a mutable in the list, or subclass list so
that indexing gets the value, looks it up in the appropriate
namespace, and returns that value.


Or something even conceptually simpler than having to muck about with
looking up different namespaces:

class Data:
def __init__(self, obj):
self.value = obj
def __str__(self):
return self.value

temp = Data(5)
L = [temp]
print L

will give 5.

temp.value = 6
print L

will now give 6.
 
M

Mike Meyer

Steven D'Aprano said:
Paul Dale said:
Hi everyone,

Is it possible to bind a list member or variable to a variable such that

temp = 5

list = [ temp ]

Don't use the names of built-in functions as variables.
temp == 6

list

would show

list = [ 6 ]

No. You need to either put a mutable in the list, or subclass list so
that indexing gets the value, looks it up in the appropriate
namespace, and returns that value.

Or something even conceptually simpler than having to muck about with
looking up different namespaces:
[elided]

Um, that's the *first* solution I proposed: putting a mutable on the
list.

<mike
 
P

Paul Dale

Thanks everyone for your comments and suggestions!

I haven't quite decided which approach I'll take, but it's nice to have
some options.

Paul

Tom said:
Is it possible to bind a list member or variable to a variable such that

temp = 5
list = [ temp ]
temp == 6
list

would show

list = [ 6 ]

As you know by now, no. Like any problem in programming, this can be
solved with a layer of abstraction: you need an object which behaves a bit
like a variable, so that you can have multiple references to it. The
simplest solution is to use a single-element list:


temp = [None] # set up the list
temp[0] = 5
list = [temp]
temp[0] = 6
list
[[6]]

I think this is a bit ugly - the point of a list is to hold a sequence of
things, so doing this strikes me as a bit of an abuse.

An alternative would be a class:

class var:
def __init__(self, value=None):
self.value = value
def __str__(self): # not necessary, but handy
return "<<" + str(self.val) + ">>"


temp = var()
temp.value = 5
list = [temp]
temp.value = 6
list
[<<6>>]

This is more heavyweight, in terms of both code and execution resources,
but it makes your intent clearer, which might make it worthwhile.

tom
 

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
474,269
Messages
2,571,338
Members
48,025
Latest member
Rigor4

Latest Threads

Top