python variable assignement

Duncan Booth duncan.booth at invalid.invalid
Mon Sep 24 09:31:34 EDT 2007


mihai <Mihai.Grecu at 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



More information about the Python-list mailing list