Tags: append, array, arraylen32, code, conversion, following, import, len16, len32, programming, python, type

Type conversion?

On Programmer » Python

4,953 words with 4 Comments; publish: Thu, 08 May 2008 00:18:00 GMT; (200109.38, « »)

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__

<type 'long'>

z = len16[0]

print z.__class__

<type 'int'>

how can I change Zs type to long?

Or how how can I change an arrays type?

All Comments

Leave a comment...

  • 4 Comments
    • 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__

      > <type 'long'>

      > z = len16[0]

      > print z.__class__

      > <type 'int'>

      > how can I change Zs type to long?

      z_long = long(z)

      type(z_long)

      <type 'long'>

      > Or how how can I change an arrays type?

      #1; Thu, 08 May 2008 00:20:00 GMT
    • Rob Cowie wrote:

      > KraftDiner wrote:

      > z_long = long(z)

      > type(z_long)

      > <type 'long'>

      >

      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.

      #2; Thu, 08 May 2008 00:21:00 GMT
    • 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...

      #3; Thu, 08 May 2008 00:22:00 GMT
    • "KraftDiner" <bobrien18.python.todaysummary.com.yahoo.com> wrote in message

      news:1155921295.608755.269970.python.todaysummary.com.i3g2000cwc.googlegroups.com...

      > 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

      #4; Thu, 08 May 2008 00:23:00 GMT