How to coerce a list of vars into a new type?

Simon Brunning simon at brunningonline.net
Mon Oct 2 17:22:30 CEST 2006


On 10/2/06, Matthew Wilson <matt at tplus1.com> wrote:
>
> I want to verify that three parameters can all be converted into
> integers, but I don't want to modify the parameters themselves.
>
> This seems to work:
>
>     def f(a, b, c):
>
>         a, b, c = [int(x) for x in (a, b, c)]
>
> Originally, I had a bunch of assert isinstance(a, int) statements at the
> top of my code, but I decided that I would rather just check if the
> parameters can be converted into integers.
>
> The new a, b, and c are all different objects, with different id values.
> Anyhow, this all seems like black magic to me.  Can anyone explain what
> is going on?

You've re-bound the names "a", "b" and "c" to new objects, so
naturally they have different ids. If you leave out the "a, b, c ="
bit, you'll leave the original names bound to the objects that have
been passed into your function as arguments.

> Is it as simple as call-by-value?

The "call-by-whatever" concepts don't really apply to Python very well
- any attempt to do so seems to result in more confusion than anything
else.

I'd recommend you take a look at
<http://effbot.org/zone/python-objects.htm>. it explains everything
far better than I could.

-- 
Cheers,
Simon B,
simon at brunningonline.net



More information about the Python-list mailing list