Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'exception': 0.03; 'type,': 0.07; 'arguments,': 0.09; 'raised.': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'sat,': 0.15; 'also:': 0.16; 'arg2,': 0.16; 'subject:object': 0.16; 'subject:type': 0.16; 'w/o': 0.16; 'string': 0.17; 'wrote:': 0.17; 'feb': 0.19; 'cc:2**0': 0.23; 'this:': 0.23; 'cc:no real name:2**0': 0.24; 'idea': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'object,': 0.27; 'chris': 0.28; '>>>>': 0.29; 'convert': 0.29; 'function': 0.30; 'handle': 0.33; 'received:google.com': 0.34; 'thanks': 0.34; '2.0': 0.35; 'fail': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'really': 0.36; 'but': 0.36; 'should': 0.36; 'too': 0.36; 'possible': 0.37; 'two': 0.37; 'why': 0.37; 'received:209': 0.37; 'received:209.85.216': 0.37; 'subject:: ': 0.38; 'list,': 0.39; 'dont': 0.64; 'great': 0.64; 'equals': 0.65; 'sounds': 0.71; '2013': 0.84 X-Received: by 10.49.58.167 with SMTP id s7mr678990qeq.5.1360409018834; Sat, 09 Feb 2013 03:23:38 -0800 (PST) Newsgroups: comp.lang.python Date: Sat, 9 Feb 2013 03:23:38 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=61.12.24.164; posting-account=1rYaPwoAAABCAKjoN8yjFR7q5GBJ_0Za References: <90002ad8-06fe-42d3-a463-a43c0b2401aa@googlegroups.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 61.12.24.164 MIME-Version: 1.0 Subject: Re: Coercing one object to type of another From: Vijay Shanker To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 45 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360415296 news.xs4all.nl 6933 [2001:888:2000:d::a6]:42801 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38518 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: 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