python variable assignement

M

mihai

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

Carsten Haese

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

mihai

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

Carsten Haese

[...]
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,
 
D

Duncan Booth

mihai said:
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
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top