Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #38502
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.008 |
| X-Spam-Evidence | '*H*': 0.98; '*S*': 0.00; 'exception': 0.03; 'arguments,': 0.09; 'raised.': 0.09; 'def': 0.10; 'sat,': 0.15; 'also:': 0.16; 'arg2,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'subject:object': 0.16; 'subject:type': 0.16; 'wrote:': 0.17; 'feb': 0.19; 'header:In- Reply-To:1': 0.25; 'object,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'received:209.85.212': 0.28; '>>>>': 0.29; 'convert': 0.29; 'function': 0.30; 'handle': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; '2.0': 0.35; 'fail': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'should': 0.36; 'possible': 0.37; 'two': 0.37; 'why': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'dont': 0.64; 'equals': 0.65; '2013': 0.84 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=v2fIkUi/dkR5mJkWa29pWCjuHv66jfpM5CRGMsZM9wY=; b=yuULxwNrMs7ZsbmSxHTUEOtEuphvQuqBbe3alUuWgLJy2OfgLDutSdj2hiuJFe4O+9 bKzU9/BIzZcwUVDJ4CEUJpk+/BtPHJ3cAdaVPbjzdqqr4YejSySx6XOrMQNzoWqWvefd YRbOOg2ZtabN19JO4LGdTEXU2MtKaNMoPy1Iu46VdhRsfMJH+eSExEJjBqp8pkrtTx+2 /eOy3FzcEnw3plHiFtDPjpJxfSS0Kg8LDflrm2SRZlshX5ai/zWYtB1we1rkzcwORosh /I/AjhZq0c3btewjjHBe9PMV4RpKTNG3Vdl/FHawPfLnWu3g9GrmWt4+7rXDY++du6rH Iz6Q== |
| MIME-Version | 1.0 |
| X-Received | by 10.52.88.197 with SMTP id bi5mr9251882vdb.58.1360406609021; Sat, 09 Feb 2013 02:43:29 -0800 (PST) |
| In-Reply-To | <90002ad8-06fe-42d3-a463-a43c0b2401aa@googlegroups.com> |
| References | <90002ad8-06fe-42d3-a463-a43c0b2401aa@googlegroups.com> |
| Date | Sat, 9 Feb 2013 21:43:28 +1100 |
| Subject | Re: Coercing one object to type of another |
| From | Chris Angelico <rosuav@gmail.com> |
| To | python-list@python.org |
| Content-Type | text/plain; charset=ISO-8859-1 |
| 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> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1534.1360406617.2939.python-list@python.org> (permalink) |
| Lines | 19 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1360406617 news.xs4all.nl 6973 [2001:888:2000:d::a6]:45831 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:38502 |
Show key headers only | View raw
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
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