471,342 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,342 software developers and data experts.

python variable assignement


I work at an application witch has embeded python.

We have an python type X.

# a != b

a = b # at this point both variables have the same value

b = select_other()

# steel the same values but both have the new value of b

What might be the cause for this behavior? The type of a and b
variable is the same,
and is defined using PyTypeObject structure.

I hope I was explicit.

Thank you in advance,
Mihai.

Sep 24 '07 #1
4 1204
On Mon, 2007-09-24 at 10:13 +0000, mihai wrote:
I work at an application witch has embeded python.

We have an python type X.

# a != b

a = b # at this point both variables have the same value
Not quite. At this point, 'a' and 'b' are names in the local namespace
that refer to the same object.
b = select_other()
Now 'b' becomes a reference to the object returned by select_other().
Presumably that's a different object than the one it referred to before.
'a' still refers to the same object it referred to before.
# steel the same values but both have the new value of b
No, the code you showed us wouldn't behave this way.
What might be the cause for this behavior?
What behavior?
The type of a and b
variable is the same,
and is defined using PyTypeObject structure.
That doesn't matter. (Variable) Names don't have types. Objects have
types, and a name can refer to an object of any type. See
http://effbot.org/zone/python-objects.htm .
I hope I was explicit.
Unfortunately, you weren't. If the above explanations didn't help you,
please explain what you want to achieve, show us your actual code and
what you expect it to do, and tell us what it's doing instead.

--
Carsten Haese
http://informixdb.sourceforge.net
Sep 24 '07 #2

This is the code:

begin = select_point()
if begin == None:
return

end = select_point()
while end != None:
record = Record(begin, end)
id = add(record)
begin = end
end = select_point()
# here (sometimes) begin has the same value (or points to the
same object) like end, the newly selected one

Is there a way to see if the names points to the same variables or
that there are different variables with the same values?

The problem is that the problem is more like an bug,
it happens only in certain conditions, and I have no idea why.

I have checked the values returned by select_point() and are different
in all the cases,
so the problem is with that variables names/values.

Thank you again,
Mihai.

Sep 24 '07 #3
On Mon, 2007-09-24 at 12:12 +0000, mihai wrote:
[...]
id = add(record)
[...]
Not that this causes your problem, but I'd still like to point out that
'id' is the name of a built-in function. Shadowing built-in names can
lead to surprising behavior.
Is there a way to see if the names points to the same variables or
that there are different variables with the same values?
Yes. "==" tests whether two objects are equal, whereas "is" tests
whether two objects are actually the same object. Examples:

Two different list objects with equal contents:
>>a = [1,2,3]
b = [1,2,3]
a==b
True
>>a is b
False

Two names for the same list object:
>>a = [1,2,3]
b = a
a==b
True
>>a is b
True

Hope this helps,

--
Carsten Haese
http://informixdb.sourceforge.net
Sep 24 '07 #4
mihai <Mi*********@gmail.comwrote:
>
This is the code:

begin = select_point()
if begin == None:
return

end = select_point()
while end != None:
record = Record(begin, end)
id = add(record)
begin = end
end = select_point()
# here (sometimes) begin has the same value (or points to the
same object) like end, the newly selected one

Is there a way to see if the names points to the same variables or
that there are different variables with the same values?
You can check whether two names refer to the same object with the 'is'
operator. So you would use:

if begin is end: continue

to skip over any duplicate
>
The problem is that the problem is more like an bug,
it happens only in certain conditions, and I have no idea why.

I have checked the values returned by select_point() and are different
in all the cases,
so the problem is with that variables names/values.
Are you sure that nothing you do can change the list of points you are
iterating over: usually iteration returning unexpected duplicates is
because you inserted something new into the middle of the list.

A few other unrelated points: the convention is to use 'is' when
checking for None, and you can reduce the number of calls to
'select_point' if you use the 'iter' function. Putting those together:

begin = select_point()
if begin is None:
return

for end in iter(select_point, None):
if begin is end:
continue
record = Record(begin, end)
id = add(record)
begin = end
Sep 24 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by pranab_bajpai | last post: by
4 posts views Thread by Michael Yanowitz | last post: by
17 posts views Thread by Alexander Eisenhuth | last post: by
145 posts views Thread by Dave Parker | last post: by
7 posts views Thread by John Doe | last post: by

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.