2

I need to input 3 integers form stdin and outut their sum. Here's what i do:

(n,l,k) = raw_input().split()
print n + l + k

Of course, i get n,l,k string concatenation, so i change this a little:

(n,l,k) = raw_input().split()
print int(n) + int(l) + int(k)

Now it's ok. But is there any way to set data type for the variables 'on the fly', so n,l,k became int whitout explicit reassigning for further use( i think of n = int(n) ) ?

UPDATE:

What if i need to input int, string, int (tuple items are of different types) ? THis means i can't do

(n,S,k) = [int(i) for i in raw_input().split()]

Any zip(), lambdas, accepting types?

Thanks in advance, Ilya

1
  • If you have mixed types, please simplify. set data type for the variables doesn't make any sense at all. You don't "magically" impose types. Your code imposes types based on what it needs. int(n) + l + int(k) is how you handle int, str, int. Commented Apr 15, 2011 at 10:13

3 Answers 3

3

Not really. You need to tell it what you want. There are some more succinct ways to do that in Python.

(n,l,k) = [int(v) for v in raw_input().split()]

This is generally true of all user input. It must be validated and converted to what the rest of your program expects.

Oh, well you can use the type objects to list the types you want. Use the fact that variables are references and you can reference a type object with a variable as well.

print [t(tok) for (t, tok) in zip((int, str, int), raw_input().split())]
Sign up to request clarification or add additional context in comments.

2 Comments

I've simplified my problem too much, so I updated the question.
I don't see any difference. But you also need to keep in mind that in Python the variables n, l, and k don't actually have a type. They are references to objects that have a type. They don't "know" what they point to.
2

I think this does what you want:

(n,l,k) = [f(i) for i,f in zip(raw_input().split(),(int,str,int))]

So we're zipping together the input values and a list (or rather a tuple) of functions that will convert the input to the required type.

What you can't do is somehow tell Python that n should always contain an int and get it to convert things automatically. Variables in Python are just names which are bound to a value - any value - and so you can't add any state to them beyond the value they are bound to.

Comments

1

Can you handle the input variables as a dict?

var_types = { 'n': int, 'S': str, 'k': int } # dict, so no given order
var_order = ('n', 'S', 'k') # a tuple with a given order of variables

input_var = dict((k, var_types[k](v)) for k, v in zip(var_order, raw_input().split()))

For given string 12 abc 34 it returns:

{'S': 'abc', 'k': 34, 'n': 12}

This way you can define the var_types at one place centrally for all input variables. The var_order will be defined for each raw_input dialogue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.