Field objects inherit from the DAL Expression class, which overloads the 
"==" operator, which produces a Query object rather than actually testing 
for equality. So, when comparing the argument passed to index() against 
Field objects in the list, you will never get a False. If the first item in 
the list is a Field object, you'll have an apparent match, which will 
return index 0. Here's a workaround:

index = [str(f) for f in fields].index(str(t.myfield))

That compares the string representations of the fields rather than the 
Field objects themselves.

Anthony

On Thursday, August 16, 2012 9:31:51 AM UTC-4, simon wrote:
>
> Please can someone explain why, for a list of fields, that 
> fields.index("anything at all") always evaluates to 0.
>
> For example this  prints 23 and 0. 
>
> t=db.todo
> fields = [t.id, t.customer, t.subject, t.completed, t.modified_by]
> if 23 in fields: print("23 is in there")
> print(fields.index("hshshshs"))
>
>
> ........what I want to do is remove a field from a list of fields   
> <fields.remove(t.customer)> just removes the first field in the list.
>
>
> On Thursday, August 16, 2012 9:31:51 AM UTC-4, simon wrote:
>
> Please can someone explain why, for a list of fields, that 
> fields.index("anything at all") always evaluates to 0.
>
> For example this  prints 23 and 0. 
>
> t=db.todo
> fields = [t.id, t.customer, t.subject, t.completed, t.modified_by]
> if 23 in fields: print("23 is in there")
> print(fields.index("hshshshs"))
>
>
> ........what I want to do is remove a field from a list of fields   
> <fields.remove(t.customer)> just removes the first field in the list.
>
>
>

-- 



Reply via email to