After taking out the class myclass stuff, the code worked for me in Python 2.6.1, through the cat line. Could you please tell us what version of Python you're running this on?

import sys
print "Python version: ", sys.version

yielded (on my machine)
Python version: 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)]

(There are two typos in Test 3, and in Test 4 you seem to be treating this object like a list.)

mark.sea...@gmail.com wrote:
On Mar 30, 10:53 am, David Bolen <db3l....@gmail.com> wrote:
mark.sea...@gmail.com writes:
class myclass(object):
#
    # def __new__(class_, init_val, size, reg_info):
    def __init__(self, init_val, size, reg_info):
# self =bject.__new__(class_)
        self.reg_info =eg_info
        print self.reg_info.message
        self.val =elf
Note that here you assign self.val to be the object itself.  Are you
sure you didn't mean "self.val =nit_val"?

(...)
    def __int__(self):
        return self.val
Instead of an integer, you return the current class instance as set up
in __init__.  The __int__ method ought to return an integer.

    def __long__(self):
        return long(self.val)
And this will be infinite recursion, since long(<obj>) will try to
call the __long__ method on <obj> so you're just recursing on the
__long__ method.

You can see this more clearly with:

    >>> cat =yclass(0x55, 32, my_reg)
    >>> int(cat)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: __int__ returned non-int (type myclass)
    >>>

I won't post the traceback for long(cat), as it's, well, "long" ...

-- David

Hi David;

Yep I had fixed up that version actually.  Here is the latest.

from ctypes import *

class REG_INFO(Structure):
    _fields_ =
        ('address', c_ubyte),
        ('message', c_char * 256),
        ('size', c_ubyte)
        ]

class myclass(object):
#
    # def __new__(class_, init_val, size, reg_info):
    def __init__(self, init_val, reg_info):

        # self =bject.__new__(class_)
        self.reg_info =eg_info
        print reg_info.message
        self.val =nit_val
        self.size =eg_info.size
        self.addr =eg_info.address
        print 'address =x%02X' % self.addr
        # return self
#
    def __getitem__(self, index): # gets a single bit
        if index >=elf.size:
            return self.val
        return (self.val >> index) & 1
#
    def __get__(self): # gets a single bit
        return self.val
#
    def __setitem__(self,index,value): # sets a single bit
        if index >=elf.size:
            self.val =alue
            return
        value     =value&1L)<<index
        mask     =1L)<<index
        self.val  =self.val & ~mask) | value
        return
#
    def __int__(self):
        return self.val
#
    def __long__(self):
        return long(self.val)

#
#
# setup
my_reg =EG_INFO()
my_reg.address =xab
my_reg.message =hello world'
my_reg.size =2

print 'TEST 1'
dog =x123456789ABCDEF0
print 'type(dog) =s' % type(dog)
print 'dog val =x%016X' % dog

print 'TEST 2'
cat =yclass(0x55, my_reg)
print 'type(cat) =s' % type(cat)
print 'cat val =x%016X' % cat

print 'TEST 3'
bird =yclass(dog, my_reg)
print 'type(bird) =s' % type(bird)
print 'bird val =x%016X' % bird

print 'TEST 4'
print bird
print 'bird[0] val =x%01X' % bird[0]
bird[0] =bird[0]
print 'bird val =x%016X' % bird
print 'bird[0] val =x%01X' % bird[0]

When Run:

TEST 1
type(dog) =type 'long'>
dog val =x123456789ABCDEF0
TEST 2
hello world
address =xAB
type(cat) =class '__main__.myclass'>
cat val =x0000000000000055
TEST 3
hello world
address =xAB
type(bird) =class '__main__.myclass'>
Traceback (most recent call last):
  File "C:\(big path)\bignum5.py", line 69, in <module>
    print 'bird val =x%016X' % bird
TypeError: int argument required

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to