473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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


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
8 1077
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
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
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
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
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
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

Oct 2 '06 #7
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: nnes | last post by:
I have really no mayor gripes, and the few things I would change would break backward compatibility, but here is the list anyway: 1.) Eliminate the whole long stuff. E.g. built-in long ()...
2
by: Steve | last post by:
Hi, I have a very long string, someting like: DISPLAY=localhost:0.0,FORT_BUFFERED=true, F_ERROPT1=271\,271\,2\,1\,2\,2\,2\,2,G03BASIS=/opt/g03b05/g03/basis,...
3
by: ken.boss | last post by:
I am a python newbie, and am grappling with a fundamental concept. I want to modify a bunch of variables in place. Consider the following: >>> a = 'one' >>> b = 'two' >>> c = 'three' >>>...
5
by: Nicolas Couture | last post by:
Hi, Is it possible to generate a list of `None' ? opts.__dict__.values() below could be represented by --- def get_options(opts): """Return True or False if an option is set or not"""...
33
by: abs | last post by:
Hi all. My list: <ul> <li id="a" onclick="show(this)">Aaaaaaaa</li> <li id="b" onclick="show(this)">Bbbbbbbb</li> <li id="c" onclick="show(this)">Cccccccc <ul> <li id="d"...
4
by: James Harris | last post by:
Having updated my Debian system it now complains that I am using an incompatible pointer type in warnings such as "passing arg 2 of 'bind' from incompatible pointer type" from code, struct...
10
by: John A Grandy | last post by:
Say I have Class1 which contains static Class2 var1 = new Class2(); Is Class2 constructor code only executed if var1 is referenced in the code-execution path ? Or is Class2 constructor code...
7
by: Jorgen Bodde | last post by:
Hi, I am trying to simplify my code, and want to automate the assigning of variables I get back from a set. I was thinking of putting the variables I want changed in a list: L = ...
4
by: mh | last post by:
I want to iterate over members of a module, something like: for i in dir(x): if type(i) == types.FunctionType: ... but of course dir() returns a list of strings. If x is a module, how can I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.