Coercing one object to type of another

Discussion in 'Python' started by Vijay Shanker, Feb 9, 2013.

  1. 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 ?
    Vijay Shanker, Feb 9, 2013
    #1
    1. Advertising

  2. On Sat, Feb 9, 2013 at 9:29 PM, 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.
    > 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
    Chris Angelico, Feb 9, 2013
    #2
    1. Advertising

  3. 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 <> 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, Feb 9, 2013
    #3
  4. 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 <> 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, Feb 9, 2013
    #4
  5. On Sat, Feb 9, 2013 at 10:23 PM, Vijay Shanker <> 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
    Chris Angelico, Feb 9, 2013
    #5
  6. 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
    Steven D'Aprano, Feb 9, 2013
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stuart Forsyth
    Replies:
    1
    Views:
    4,910
    vincent wehren
    Nov 13, 2003
  2. Randall Parker
    Replies:
    2
    Views:
    802
    Fredrik Lundh
    Mar 22, 2006
  3. Max Erickson
    Replies:
    0
    Views:
    1,255
    Max Erickson
    Jun 21, 2006
  4. Fredrik Lundh
    Replies:
    0
    Views:
    765
    Fredrik Lundh
    Jun 21, 2006
  5. Jon Bowlas
    Replies:
    6
    Views:
    20,068
    Jon Bowlas
    Jul 27, 2006
Loading...

Share This Page