473,398 Members | 2,212 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,398 software developers and data experts.

Binding a variable?


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
8 1738
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
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
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
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
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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jonathan | last post by:
I'm puzzled by Python's behavior when binding local variables which are introduced within exec() or execfile() statements. First, consider this simple Python program: # main.py def f() : x = 1...
1
by: JD Kronicz | last post by:
Hi .. I have an issue I have been beating my head against the wall on for some time. I am trying to use late binding for MS graph so that my end users don't have to worry about having the right...
2
by: Gastin | last post by:
I am consuming a web serivce from Amazon.Com. I have the following class which was autogenerated by VS.NET when I created a Web Reference to...
2
by: mark | last post by:
I understand that writing programs with option strict on is the best way to obtain stable applications. I have also found the applications to run much faster. Option strict on disallows late...
30
by: lgbjr | last post by:
hi All, I've decided to use Options Strict ON in one of my apps and now I'm trying to fix a late binding issue. I have 5 integer arrays: dim IA1(500), IA2(500), IA3(500), IA4(500), IA5(500) as...
9
by: Miro | last post by:
VB 2003 and Im still new to vb, so i hope i can explain this as best I can. I have a variable defined as such: ( simple example ) Dim AVariableOfSorts(,) As Object = _ { _ {"Last", "String",...
3
by: Simon Tamman | last post by:
I've come across an interesting bug. I have workarounds but i'd like to know the root of the problem. I've stripped it down into a short file and hope someone might have an idea about what's going...
0
by: manir | last post by:
Hi, It would really be of great help if you could suggest a solution for me. Program: I need to execute a stored procedure from an Oracle server through a C++ program using oci.h (Oracle...
3
by: =?Utf-8?B?Sm9obiBCdW5keQ==?= | last post by:
New to databinding in vs2005, I always did it manually in 2003. I have no problem loading comboboxes, and a change in that combobox changes the data in the textboxes but I can not figure out a way...
3
ADezii
by: ADezii | last post by:
The process of verifying that an Object exists and that a specified Property or Method is valid is called Binding. There are two times when this verification process can take place: during compile...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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.