Velocity Reviews > Coercing one object to type of another

# Coercing one object to type of another

Vijay Shanker
Guest
Posts: n/a

 02-09-2013
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:
>>> int('2')

2
>>> float('2.0')

2.0
>>> coerce(2,2.0)

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

Chris Angelico
Guest
Posts: n/a

 02-09-2013
On Sat, Feb 9, 2013 at 9:29 PM, Vijay Shanker <(E-Mail Removed)> wrote:
> 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:
>>>> int('2')

> 2
>>>> float('2.0')

> 2.0
>>>> coerce(2,2.0)

> (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

Vijay Shanker
Guest
Posts: n/a

 02-09-2013
On Saturday, February 9, 2013 4:13:28 PM UTC+5:30, Chris Angelico wrote:
> On Sat, Feb 9, 2013 at 9:29 PM, Vijay Shanker <(E-Mail Removed)> wrote:
>
> > 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:

>
> >>>> int('2')

>
> > 2

>
> >>>> float('2.0')

>
> > 2.0

>
> >>>> coerce(2,2.0)

>
> > (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

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

Vijay Shanker
Guest
Posts: n/a

 02-09-2013
On Saturday, February 9, 2013 4:13:28 PM UTC+5:30, Chris Angelico wrote:
> On Sat, Feb 9, 2013 at 9:29 PM, Vijay Shanker <(E-Mail Removed)> wrote:
>
> > 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:

>
> >>>> int('2')

>
> > 2

>
> >>>> float('2.0')

>
> > 2.0

>
> >>>> coerce(2,2.0)

>
> > (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

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

Chris Angelico
Guest
Posts: n/a

 02-09-2013
On Sat, Feb 9, 2013 at 10:23 PM, Vijay Shanker <(E-Mail Removed)> wrote:
> 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

Steven D'Aprano
Guest
Posts: n/a

 02-09-2013
Vijay Shanker wrote:

> 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:
> >>> int('2')

> 2
> >>> float('2.0')

> 2.0
> >>> coerce(2,2.0)

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

--
Steven