f



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.

0
9/24/2007 10:13:42 AM
comp.lang.python 77058 articles. 5 followers. Post Follow

4 Replies
198 Views

Similar Articles

[PageSpeed] 6

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


0
carsten7356 (552)
9/24/2007 10:35:34 AM
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.

0
9/24/2007 12:12:24 PM
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


0
carsten7356 (552)
9/24/2007 12:31:53 PM
mihai <Mihai.Grecu@gmail.com> wrote:

> 
> 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
0
duncan.booth (1734)
9/24/2007 1:31:34 PM
Reply: