starred expression with tuple or array !? differente behaviour!

Skip to first unread message

António Ramos

Apr 18, 2022, 12:20:49 PMApr 18
to py4web,
Hello i have my code to generate html base on a condition

one of the table columns should be created only if var =1 

this works but the html removes the _class="aa" in case of var==1, and does not create the TD if var!=1
*(DIV("b",_class="aa") ) if var==1 else tuple())

Using a list
If i use a list it shows the _class="aa" in case of var==1 but how do i make it to ommit the column in case var!=1 ??
It creates a blank TD() and that is not what i want 
TD(*[DIV("b",_class="aa") if var==1 else None???])


Jim S

Apr 18, 2022, 2:47:17 PMApr 18
to web2py-users
Did you see my reply in the py4web group?


If I'm understanding the question correctly, I think I'd do the following:

cell_list = []
cell_list.append(TD("...first td here..."))
cell_list.append(TD("...additional cell..."))
cell_list.append(TD("...additional cell..."))

if var == 1:
   cell_list.append('my conditional cell')

cell_list.append(TD("...remaining cells..."))
cell_list.append(TD("...remaining cells..."))
cell_list.append(TD("...remaining cells..."))

Or, if you want to specify all the standard cells at one time...

cell_list = [TD(), TD(), TD(), TD(), TD())
if var == 1:
    cell_list.insert(3, TD('conditional cell'))

Does that help?


Reply all
Reply to author
0 new messages