Coercing one object to type of another
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 ? -- http://mail.python.org/mailman/listinfo/python-list
Re: Coercing one object to type of another
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 deont...@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 -- http://mail.python.org/mailman/listinfo/python-list
keyerror '__repr__'
hi i have this class book class book: def __init__(self,name,price): self.name = name self.price = price def __getattr__(self,attr): if attr == '__str__': print 'intercepting in-built method call ' return '%s:%s' % (object.__getattribute__(self,'name'),object.__getattribute___(self,'price')) else: return self.__dict__[attr] b = book('the tempest',234) b Traceback (most recent call last): File console, line 1, in module File console, line 11, in __getattr__ KeyError: '__repr__' i am missing on a concept here. please enlighten me. -- http://mail.python.org/mailman/listinfo/python-list
Python Learning Environment
Hi, My Linux box is ubuntu system. I want to create a development environment on my system for python programing language. I got to see there are two versions of python language 1. python 2.5.6 2. python 3.1.2 To find out what version i look in to my /usr/bin folder. There are many entries for python command - python - python2 - python2.5 - python2.6 - python3 - python3.1 what does this mean? I am able to run run my first program with all these command. should i remove all these and have the latest one? I am confused about these finding. Is this okay to have these all? Regards, Vijay Shanker Dubey -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Learning Environment
Thanks friend, Got the point. Regards, Vijay Shanker Dubey On Sat, Apr 17, 2010 at 11:31 PM, Krister Svanlund krister.svanl...@gmail.com wrote: On Sat, Apr 17, 2010 at 7:06 PM, Vijay Shanker Dubey vijay.s...@gmail.com wrote: Yes you are right about symlink thing. So what should I do for a clever developer environment? Should I change that python link to python3 or python3.1? Regards, Vijay Shanker Dubey It all depends on what you want to do. I would say that you shouldn't change your python link at all, if you want to run a python script using 3.1 just call the script using python3 as an interpreter. -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list