global name 'self' is not defined - noob trying to learn

mark.seagoe at gmail.com mark.seagoe at gmail.com
Mon Mar 30 18:01:51 CEST 2009


Here again is example:


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, size, reg_info):

        # self = object.__new__(class_)
        self.reg_info = reg_info
        print self.reg_info.message
        self.val = self
        self.size = reg_info.size
        print 'address = 0x%02X' % self.reg_info.address
        # return self
#
    def __getitem__(self, index): # gets a single bit
        if index >= self.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 >= self.size:
            self.val = value
            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 = REG_INFO()
my_reg.address = 0xab
my_reg.message = 'hello world'

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

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

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

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


Here is how it currently runs:
C:\path...
TEST 1
type(dog) = <type 'long'>
dog val = 0x123456789ABCDEF0
TEST 2
hello world
address = 0xAB
type(cat) = <class '__main__.myclass'>
Traceback (most recent call last):
  File "C:\path... \bignum5.py", line 62, in <module>

    print 'cat val = 0x%016X' % cat
TypeError: int argument required

Thanks,
Mark



More information about the Python-list mailing list