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

M

Matthew Wilson

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?
 
S

Simon Brunning

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.
 
B

bearophileHUGS

Matthew said:
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
 
R

Robert Kern

Matthew said:
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
 
B

bearophileHUGS

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
 
G

Guest

(e-mail address removed):
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.
 
B

bearophileHUGS

Björn Lindström:
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
 
R

Robert Kern

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
 
V

vbgunz

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
# ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top