I tried erlang-ish [1|2] in python and something came out...

Chris Rebert clp at rebertia.com
Sun Sep 21 13:13:08 EDT 2008


On Sun, Sep 21, 2008 at 10:02 AM, process <circularfunc at gmail.com> wrote:
> In erlang you can cons like this: [1|2]. i tried this in python and it
> didnt raise an error but i dont know what the result do

In Python, like in C, the "|" operator on integers performs a bitwise-OR.
To "cons" nondestructively onto a list, do e.g. [head_value] + tail,
but note that this isn't very idiomatic.
CPython's lists are implemented internally as arrays, not linked
lists, hence why there's no "cons" operator in Python.
You may also be interested in list.append()

Regards,
Chris

>
>>>> [1|2]
> [3]
>>>> [2|2]
> [2]
>>>> a = [2|2]
>>>> a
> [2]
>>>> [2|3]
> [3]
>>>> [2|1]
> [3]
>>>>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
Follow the path of the Iguana...
http://rebertia.com



More information about the Python-list mailing list