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

David Bolen db3l.net at gmail.com
Mon Mar 30 13:53:48 EDT 2009


mark.seagoe 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



More information about the Python-list mailing list