python * function in Html Helpers

64 views
Skip to first unread message

António Ramos

unread,
Sep 13, 2011, 1:10:24 PM9/13/11
to web...@googlegroups.com
Hello, i dont understant quite well how to interpret the *  in this code:


table = [['a', 'b'], ['c', 'd']]
2 >>> print TABLE(*[TR(*rows) for rows in table])
3 <table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>

Can someone please have the kindness to better understand it 

I read the book but still not get it



thank you

Jonathan Lundell

unread,
Sep 13, 2011, 1:48:35 PM9/13/11
to web...@googlegroups.com
On Sep 13, 2011, at 10:10 AM, António Ramos wrote:

Hello, i dont understant quite well how to interpret the *  in this code:

Anthony

unread,
Sep 13, 2011, 1:51:42 PM9/13/11
to web...@googlegroups.com
In Python, if you have a list or tuple, you can precede it with an "*" and pass it as an argument to a function, and the function will treat it as if each item in the list were passed as a separate positional argument (same thing for preceding a dictionary with "**", which will treat the dictionary as a set of separate keyword arguments).

In this example, 'rows' is a list, so TR(*rows) is like doing TR(rows[0],rows[1]). In this case, it's not much shorter, but the *rows syntax is handy when rows contains many elements, especially when the number of elements is determined dynamically (e.g., with a database select).

[TR(*rows) for rows in table] is a list comprehension, which results in a list of TR objects, so the "*" preceding it is equivalent to:

TABLE(TR(table[0][0],table[0][1]),TR(table[1][0],table[1][1]))

Anthony

Noel Villamor

unread,
Sep 13, 2011, 6:11:42 PM9/13/11
to web2py-users


I got the * and the ** explanations but António is asking about ***.


On Sep 14, 5:51 am, Anthony <abasta...@gmail.com> wrote:
> In Python, if you have a list or tuple, you can precede it with an "*" and
> pass it as an argument to a function, and the function will treat it as if
> each item in the list were passed as a separate positional argument (same
> thing for preceding a dictionary with "**", which will treat the dictionary
> as a set of separate keyword arguments).
>
> In this example, 'rows' is a list, so TR(*rows) is like doing
> TR(rows[0],rows[1]). In this case, it's not much shorter, but the *rowssyntax is handy when rows
> contains many elements, especially when the number of elements is determined
> dynamically (e.g., with a database select).
>
> [TR(*rows) for rows in table] is a list comprehension, which results in a
> list of TR objects, so the "*" preceding it is equivalent to:
>
> TABLE(TR(table[0][0],table[0][1]),TR(table[1][0],table[1][1]))
>
> Anthony
>
>
>
>
>
>
>
> On Tuesday, September 13, 2011 1:10:24 PM UTC-4, Ramos wrote:
>
> > Hello, i dont understant quite well how to interpret the ***  in this
> > code:
>
> > table = [['a', 'b'], ['c', 'd']]
> > 2 >>> print TABLE(***[TR(***rows) for rows in table])

Jonathan Lundell

unread,
Sep 13, 2011, 6:19:56 PM9/13/11
to web...@googlegroups.com
On Sep 13, 2011, at 3:11 PM, Noel Villamor wrote:

> I got the * and the ** explanations but António is asking about ***.

It was just * in the original message. Must have gotten changed in the replies or something.

Anthony

unread,
Sep 13, 2011, 6:25:18 PM9/13/11
to web...@googlegroups.com
On Tuesday, September 13, 2011 6:11:42 PM UTC-4, Noel Villamor wrote:


I got the * and the ** explanations but António is asking about ***.

Where do you see any mention of ***?

António Ramos

unread,
Sep 13, 2011, 6:45:42 PM9/13/11
to web...@googlegroups.com
Not only web2py is the best framework but this forum is also the best i have ever participated.

One for all and all for one.

Thank you ALL for your time

António

2011/9/13 Anthony <abas...@gmail.com>

Noel Villamor

unread,
Sep 14, 2011, 3:59:01 PM9/14/11
to web2py-users

Oh.

This is what I see:

"""
Hello, i dont understant quite well how to interpret the *** in this
code:

table = [['a', 'b'], ['c', 'd']]
2 >>> print TABLE(***[TR(***rows) for rows in table])
"""

If my eyes don't deceive me, I do see *** in three locations.

:)


Noel

António Ramos

unread,
Sep 14, 2011, 5:03:05 PM9/14/11
to web...@googlegroups.com
What the hell, that was not what i wrote
Only one *

You have a problem in your email.

2011/9/14 Noel Villamor <noe...@gmail.com>

Anthony

unread,
Sep 14, 2011, 5:07:10 PM9/14/11
to web...@googlegroups.com
Actually, it shows up as *** in the old Google Groups interface: https://groups.google.com/group/web2py/browse_frm/thread/f4475684a88fef17/45622d797458e974?hl=en#45622d797458e974. The new interface correctly displays only one *. I use the new interface, so saw the intended single *.

Anthony

Noel Villamor

unread,
Sep 14, 2011, 5:23:28 PM9/14/11
to web2py-users

Interesting!

The old:

https://groups.google.com/group/web2py/browse_frm/thread/f4475684a88fef17/45622d797458e974?hl=en#45622d797458e974

The new:

https://groups.google.com/forum/#!topic/web2py/9EdWhKiP7xc

Thanks for clearing up the confusion Anthony.

Noel


On Sep 15, 9:07 am, Anthony <abasta...@gmail.com> wrote:
> Actually, it shows up as *** in the old Google Groups
> interface:https://groups.google.com/group/web2py/browse_frm/thread/f4475684a88f....
Reply all
Reply to author
Forward
0 new messages