Re: Type conversion?
KraftDiner wrote: > I have the following code... > > import array > len32 = array.array('L') > len16 = array.array('H') > > len32.append(0) > len16.append(0) > > y = len32[0] > print y.__class__ > > z = len16[0] > print z.__class__ > > > how can I change Zs type to long? z_long = long(z) type(z_long) > Or how how can I change an arrays type? -- http://mail.python.org/mailman/listinfo/python-list
Re: Type conversion?
Rob Cowie wrote: > KraftDiner wrote: > > I have the following code... > > > > import array > > len32 = array.array('L') > > len16 = array.array('H') > > > > len32.append(0) > > len16.append(0) > > > > y = len32[0] > > print y.__class__ > > > > z = len16[0] > > print z.__class__ > > > > > > how can I change Zs type to long? > > z_long = long(z) > type(z_long) > > > > Or how how can I change an arrays type? In C++ you can cast one class type to another if you override the operator= Then you can convert one class type to another... In Python it would appear that the left hand side of the assignment operator is not used to determine if a cast is necessary. So how would I do this in python? a = classA() b = classB() b = a In the end b would end up just being a refrence to a no conversion would have been done. -- http://mail.python.org/mailman/listinfo/python-list
Re: Type conversion?
KraftDiner wrote: > In C++ you can cast one class type to another if you override the > operator= > Then you can convert one class type to another... > In Python it would appear that the left hand side of the assignment > operator > is not used to determine if a cast is necessary. > So how would I do this in python? > > a = classA() > b = classB() > b = a > > In the end b would end up just being a refrence to a (You aren't quite correct there: the name 'b' would refer to the same object as is referred to by the name 'a'. However there is nothing associating the two names 'b' and 'a', so no reference from 'b' to 'a'.) > no conversion would have been done. You just have to call the appropriate constructor: b = classB(a) So long as the constructor for classB knows how to create an instance from a classA instance this will have the desired effect. So: aStr = str(anInt) aFloat = float(anInt) ...and so on... -- http://mail.python.org/mailman/listinfo/python-list
Re: Type conversion?
"KraftDiner" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > > In C++ you can cast one class type to another if you override the > operator= > Then you can convert one class type to another... > In Python it would appear that the left hand side of the assignment > operator > is not used to determine if a cast is necessary. > So how would I do this in python? Start by repeating 50 times: "Python is not C++". In Python, '=' is not an operator, it is a name binder, and is defined at the language level, not at the class level. > > a = classA() > b = classB() > b = a > > In the end b would end up just being a refrence to a > no conversion would have been done. > Just as you say, '=' will completely rebind the rhs value to the lhs name. Now one thing you *could* do is to do something tricky, like use one of the overridable inplace operators, like "<<=", which *is* definable by class. Here is a type casting class that does something like you are asking for. (Back in the CORBA days, <<= was used in the C++ binding to insert values into a CORBA::Any variable - I guess it looks like some sort of typographic hypodermic needle...) -- Paul class Caster(object): def __init__(self,typ): self.castType = typ self.value = self.castType() def cast(self,other): try: return self.castType(other) except Exception,e: print "Failed to cast '%s' as %s" % (other, self.castType.__name__) # define behavior for <<= operator def __ilshift__(self,other): self.value = self.cast(other) return self def __str__(self): return "(%s) %s" % (self.castType.__name__,self.value) z = Caster(int) print z z <<= 100 print z z <<= 3.14159 print z z <<= 'what the heck?' print z Prints: (int) 0 (int) 100 (int) 3 Failed to cast 'what the heck?' as int (int) None -- http://mail.python.org/mailman/listinfo/python-list