By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,097 Members | 1,580 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,097 IT Pros & Developers. It's quick & easy.

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

P: n/a

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?

Is it as simple as call-by-value?


--
A better way of running series of SAS programs:
http://overlook.homelinux.net/wilson...asAndMakefiles
Oct 2 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On 10/2/06, Matthew Wilson <ma**@tplus1.comwrote:
>
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,
si***@brunningonline.net
Oct 2 '06 #2

P: n/a
Matthew Wilson wrote:
I want to verify that three parameters can all be converted into
integers, but I don't want to modify the parameters themselves.
To do what you need you can try this:

def f(a, b, c):
map(int, [a, b, c])
...code...

Bye,
bearophile

Oct 2 '06 #3

P: n/a
be************@lycos.com wrote:
Matthew Wilson wrote:
>I want to verify that three parameters can all be converted into
integers, but I don't want to modify the parameters themselves.

To do what you need you can try this:

def f(a, b, c):
map(int, [a, b, c])
...code...
That won't do anything since the names a, b, and c are never rebound.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Oct 2 '06 #4

P: n/a
Me:
def f(a, b, c):
map(int, [a, b, c])
...code...
Robert Kern:
That won't do anything since the names a, b, and c are never rebound.
I think you are wrong. Try to give a "35.0" or a "hello" to that
function f, and see the results.

What it does is to: "verify that three parameters can all be converted
into integers, but it doesn't modify the parameters themselves" as the
OP asked.

Bye,
bearophile

Oct 2 '06 #5

P: n/a
be************@lycos.com:
def f(a, b, c):
map(int, [a, b, c])
...code...

What it does is to: "verify that three parameters can all be converted
into integers, but it doesn't modify the parameters themselves" as the
OP asked.
I would probably put that map in an assert statement, to make the
intention clearer.

--
Björn Lindström <bk**@stp.lingfil.uu.se>
Student of computational linguistics, Uppsala University, Sweden
Oct 2 '06 #6

P: n/a
Bjrn Lindstrm:
I would probably put that map in an assert statement, to make the
intention clearer.
Isn't putting an explanation comment before that line enough?

Bye,
bearophile

Oct 2 '06 #7

P: n/a
be************@lycos.com wrote:
Me:
>>def f(a, b, c):
map(int, [a, b, c])
...code...

Robert Kern:
>That won't do anything since the names a, b, and c are never rebound.

I think you are wrong. Try to give a "35.0" or a "hello" to that
function f, and see the results.

What it does is to: "verify that three parameters can all be converted
into integers, but it doesn't modify the parameters themselves" as the
OP asked.
Point.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Oct 2 '06 #8

P: n/a
I want to verify that three parameters can all be converted into
integers, but I don't want to modify the parameters themselves.
You can twist and tweak this version OR completely redo it. In this
version a list of the conversions are returned *but* if you want to
only check if such a conversion can be made, you can return a bool
result instead of the list. Also integers are converted to floats. just
wrap the float() in an int() if you only want ints instead and wrap
again with round() or mix and do as you please to get the results you
want.
def convertToInteger(*args):
''' try to convert arguments to integers and return them in a list'''

try:
return [float(x) for x in (args)]
except ValueError:
print 'convertToInteger: pass compatible args OR return a default!'
return
funcResult = convertToInteger('1', '2', '3')
if funcResult: # if conversion was perfect, choose to do what you want
# ...
a, b, c = funcResult # change global variables!
x, y, z = funcResult # create three new variables!
print a, b, c
print x, y, z
# ...

Oct 2 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.