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

mark.seagoe at gmail.com mark.seagoe at gmail.com
Mon Mar 30 14:03:14 EDT 2009


On Mar 30, 10:53 am, David Bolen <db3l.... at gmail.com> wrote:
> mark.sea... at gmail.com writes:
> > 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
>
> Note that here you assign self.val to be the object itself.  Are you
> sure you didn't mean "self.val = init_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 = myclass(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 = object.__new__(class_)
        self.reg_info = reg_info
        print reg_info.message
        self.val = init_val
        self.size = reg_info.size
        self.addr = reg_info.address
        print 'address = 0x%02X' % self.addr
        # 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'
my_reg.size = 32

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

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

print 'TEST 3'
bird = myclass(dog, 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]

When Run:

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



More information about the Python-list mailing list