Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #38518
| 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 | <deontics@gmail.com> |
| 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 | <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 |
| References | <90002ad8-06fe-42d3-a463-a43c0b2401aa@googlegroups.com> <mailman.1534.1360406617.2939.python-list@python.org> |
| 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 <deontics@gmail.com> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Message-ID | <mailman.1546.1360415296.2939.python-list@python.org> (permalink) |
| 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 |
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