Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #38508
| X-Received | by 10.224.189.78 with SMTP id dd14mr5106835qab.0.1360409018854; Sat, 09 Feb 2013 03:23:38 -0800 (PST) |
|---|---|
| X-Received | by 10.49.58.167 with SMTP id s7mr678990qeq.5.1360409018834; Sat, 09 Feb 2013 03:23:38 -0800 (PST) |
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.glorb.com!p13no5880782qai.0!news-out.google.com!k2ni21154qap.0!nntp.google.com!p13no4633131qai.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail |
| Newsgroups | comp.lang.python |
| Date | Sat, 9 Feb 2013 03:23:38 -0800 (PST) |
| In-Reply-To | <mailman.1534.1360406617.2939.python-list@python.org> |
| Complaints-To | groups-abuse@google.com |
| Injection-Info | glegroupsg2000goo.googlegroups.com; posting-host=61.12.24.164; posting-account=1rYaPwoAAABCAKjoN8yjFR7q5GBJ_0Za |
| NNTP-Posting-Host | 61.12.24.164 |
| References | <90002ad8-06fe-42d3-a463-a43c0b2401aa@googlegroups.com> <mailman.1534.1360406617.2939.python-list@python.org> |
| User-Agent | G2/1.0 |
| MIME-Version | 1.0 |
| Message-ID | <05481ed7-ee0c-4cbd-9490-ada64735cb90@googlegroups.com> (permalink) |
| Subject | Re: Coercing one object to type of another |
| From | Vijay Shanker <deontics@gmail.com> |
| Cc | python-list@python.org |
| Injection-Date | Sat, 09 Feb 2013 11:23:38 +0000 |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Xref | csiph.com comp.lang.python:38508 |
Show key headers only | View raw
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 <deontics@gmail.com> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Coercing one object to type of another Vijay Shanker <deontics@gmail.com> - 2013-02-09 02:29 -0800
Re: Coercing one object to type of another Chris Angelico <rosuav@gmail.com> - 2013-02-09 21:43 +1100
Re: Coercing one object to type of another Vijay Shanker <deontics@gmail.com> - 2013-02-09 03:23 -0800
Re: Coercing one object to type of another Chris Angelico <rosuav@gmail.com> - 2013-02-09 22:31 +1100
Re: Coercing one object to type of another Vijay Shanker <deontics@gmail.com> - 2013-02-09 03:23 -0800
Re: Coercing one object to type of another Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-10 08:11 +1100
csiph-web