Is '[' a function or an operator or an language feature?

Paul McGuire ptmcg at austin.rr.com
Sat Jul 17 09:57:06 EDT 2010


On Jul 16, 12:01 pm, Peng Yu <pengyu... at gmail.com> wrote:
> I mean to get the man page for '[' like in the following code.
>
> x=[1,2,3]
>
> But help('[') doesn't seem to give the above usage.
>
> ###########
> Mutable Sequence Types
> **********************
>
> List objects support additional operations that allow in-place
> modification of the object. Other mutable sequence types (when added
> to the language) should also support these operations. Strings and
> tuples are immutable sequence types: such objects cannot be modified
> once created. The following operations are defined on mutable sequence
> types (where *x* is an arbitrary object):
> ...
> ##########
>
> I then checked help('LISTLITERALS'), which gives some description that
> is available from the language reference. So '[' in "x=[1,2,3]" is
> considered as a language feature rather than a function or an
> operator?
>
> ############
> List displays
> *************
>
> A list display is a possibly empty series of expressions enclosed in
> square brackets:
>
>    list_display        ::= "[" [expression_list | list_comprehension] "]"
>    list_comprehension  ::= expression list_for
>    list_for            ::= "for" target_list "in" old_expression_list
> [list_iter]
>    old_expression_list ::= old_expression [("," old_expression)+ [","]]
>    list_iter           ::= list_for | list_if
>    list_if             ::= "if" old_expression [list_iter]
> .....
> ###########
> --
> Regards,
> Peng

Also look for __getitem__ and __setitem__, these methods defined on
your own container classes will allow you to write "myobject['x']" and
have your own custom lookup code get run.

-- Paul



More information about the Python-list mailing list