Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #38511

Re: Coercing one object to type of another

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.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.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'argument': 0.04; 'arguments': 0.07; 'builtin': 0.07; 'type,': 0.07; 'callable': 0.09; 'oh,': 0.09; 'str,': 0.09; 'def': 0.10; '(the': 0.15; 'sat,': 0.15; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'subject:object': 0.16; 'subject:type': 0.16; 'w/o': 0.16; 'string': 0.17; 'wrote:': 0.17; 'feb': 0.19; 'this:': 0.23; 'idea': 0.24; 'header:In-Reply-To:1': 0.25; 'message- id:@mail.gmail.com': 0.27; 'actual': 0.28; 'case).': 0.29; 'convert': 0.29; 'switch': 0.32; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'pm,': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'really': 0.36; 'too': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'list,': 0.39; 'telling': 0.61; 'great': 0.64; 'sounds': 0.71; '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=wLxzQVItuRCvjsMhKlfrXwW25nDfaYi32DoK6X32eUM=; b=KiZWtzF2qyMrlZNl2C5lTuwQHBBtzY5B7nOHTb2Ak83n7uGPjIXn9CcQOChk1YqQ5i 0TGi3NSbESTgqobDA0pwrbtxR2xpj1JA6xSx1HkCqFVQo6mTnrH0VKSohtSUlRgyJZmR CUAnqV+vu88ASJqHgBHwkkUC6MacHdtE8Vc3CYCZeHge+8UK23W7SDqa4knqUsgwzpQl I5Au2ZT5L/3sQ3kl3HV39OKHRvMeSBerOjupwj36/pvG3FEQ1bVj7J9che/1Xee7e3yQ Ou/GcQt/ux+Woxu4uufjooZvPdX9YANqZ+TTNg+uCEKTacz8GN3u10QaaDySDbZODyYq iwvw==
MIME-Version 1.0
X-Received by 10.220.156.10 with SMTP id u10mr10812027vcw.28.1360409461436; Sat, 09 Feb 2013 03:31:01 -0800 (PST)
In-Reply-To <05481ed7-ee0c-4cbd-9490-ada64735cb90@googlegroups.com>
References <90002ad8-06fe-42d3-a463-a43c0b2401aa@googlegroups.com> <mailman.1534.1360406617.2939.python-list@python.org> <05481ed7-ee0c-4cbd-9490-ada64735cb90@googlegroups.com>
Date Sat, 9 Feb 2013 22:31:01 +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.1542.1360409469.2939.python-list@python.org> (permalink)
Lines 18
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1360409469 news.xs4all.nl 6944 [2001:888:2000:d::a6]:44188
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:38511

Show key headers only | View raw


On Sat, Feb 9, 2013 at 10:23 PM, Vijay Shanker <deontics@gmail.com> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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