By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,095 Members | 1,586 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,095 IT Pros & Developers. It's quick & easy.

Binding a variable?

P: n/a

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
Oct 21 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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].

Paul Dale wrote:
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


Oct 21 '05 #2

P: n/a
Paul Dale wrote:

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.
temp = 5
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
Oct 21 '05 #3

P: n/a
Paul Dale <pd@traxon.com> writes:
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
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Oct 21 '05 #4

P: n/a
Paul Dale wrote:
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

Oct 21 '05 #5

P: n/a
On Fri, 21 Oct 2005, Paul Dale wrote:
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

--
Transform your language.
Oct 21 '05 #6

P: n/a
On Fri, 21 Oct 2005 13:33:18 -0400, Mike Meyer wrote:
Paul Dale <pd@traxon.com> writes:
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.
--
Steven.

Oct 23 '05 #7

P: n/a
Steven D'Aprano <st***@REMOVETHIScyber.com.au> writes:
On Fri, 21 Oct 2005 13:33:18 -0400, Mike Meyer wrote:
Paul Dale <pd@traxon.com> writes:
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
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Oct 23 '05 #8

P: n/a

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 Anderson wrote:
On Fri, 21 Oct 2005, Paul Dale wrote:
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

Oct 24 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.