Coercing one object to type of another

V

Vijay Shanker

Hi
Inside a function i get a two arguments, say arg1 and arg2, how can i convert arg2 to same type as arg1 ?
I dont know type of arg1 or arg2 for that matter, I just want to convert arg2 to type of arg1 if possible and handle the exception if raised.
Also:(2.0,2.0)
but coerce('2',2) fails.If int('2') equals 2, why should it fail ?
 
C

Chris Angelico

Hi
Inside a function i get a two arguments, say arg1 and arg2, how can i convert arg2 to same type as arg1 ?
I dont know type of arg1 or arg2 for that matter, I just want to convert arg2 to type of arg1 if possible and handle the exception if raised.
Also:
(2.0,2.0)
but coerce('2',2) fails.If int('2') equals 2, why should it fail ?

You can get the type of any object, and call that:

def coerce(changeme,tothis):
return type(tothis)(changeme)

ChrisA
 
V

Vijay Shanker

You can get the type of any object, and call that:



def coerce(changeme,tothis):

return type(tothis)(changeme)



ChrisA

well it will always return me this:
<type 'str'>

what i want is if i know arg1 is of string type(and it can be of any type, say list, int,float) and arg2 is of any type, how can i convert it to type of arg1,
if arg1='hello world', type(arg1).__name__ will give me 'str', can i use this to convert my arg2 to this type, w/o resorting to if-elif conditions as there will be too many if-elif-else and it doesn really sounds a great idea !
thanks
 
V

Vijay Shanker

You can get the type of any object, and call that:



def coerce(changeme,tothis):

return type(tothis)(changeme)



ChrisA

well it will always return me this:
<type 'str'>

what i want is if i know arg1 is of string type(and it can be of any type, say list, int,float) and arg2 is of any type, how can i convert it to type of arg1,
if arg1='hello world', type(arg1).__name__ will give me 'str', can i use this to convert my arg2 to this type, w/o resorting to if-elif conditions as there will be too many if-elif-else and it doesn really sounds a great idea !
thanks
 
C

Chris Angelico

well it will always return me this:
<type 'str'>

what i want is if i know arg1 is of string type(and it can be of any type, say list, int,float) and arg2 is of any type, how can i convert it to type of arg1,
if arg1='hello world', type(arg1).__name__ will give me 'str', can i use this to convert my arg2 to this type, w/o resorting to if-elif conditions as there will be too many if-elif-else and it doesn really sounds a great idea !

Oh, okay. Then switch the order of the arguments in what I posted:

def coerce(target,convertme):
return type(target)(convertme)

You don't need to worry about the actual name of the type. It's
telling you that it's <type 'str'>; that's an actual callable object
(the same as the builtin name str, in this case). You can then call
that to coerce the other argument to that type.

ChrisA
 
S

Steven D'Aprano

Vijay said:
Hi
Inside a function i get a two arguments, say arg1 and arg2, how can i
convert arg2 to same type as arg1 ? I dont know type of arg1 or arg2 for
that matter, I just want to convert arg2 to type of arg1 if possible and
handle the exception if raised.

How do you propose to handle the exception?

If arg1 is a dict, and arg2 is an int, what do you think should happen?

convert({}, 45)
=> returns what?


If arg1 is a HTTP connection object, and arg2 is a function object, what do
you think should happen?

import urllib2
conn = urllib2.urlopen("http://www.python.com/")
convert(conn, lambda x: len(x) + 1)
=> returns what?


You cannot convert arbitrary objects of one type into some other arbitrary
type, it simply doesn't make sense. So first of all you need to decide:

- what kinds of objects do you care about?

- when given some other kind of object, how do you propose to deal with it?

The usual answer to the second question is "raise an exception".


Also:
(2.0,2.0)
but coerce('2',2) fails.If int('2') equals 2, why should it fail ?

Because '2' is a string, not a number, and coerce only operates on numbers.
What would you expect coerce("three", 2) to return? How about
coerce("apple", 2)?


Why do you think you need coerce? What are you actually trying to
accomplish? If you have two numbers, normally you would just do arithmetic
on them and let Python do any coercion needed. And if you have a number and
something else, you can't magically turn non-numbers into numbers.
 

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,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top