global name 'self' is not defined - noob trying to learn
Dave Angel
davea at ieee.org
Mon Mar 30 18:25:02 EDT 2009
Sorry, I meant I took out the stuff *before* the definition of class
myclass, and also those things that referred to it. I didn't want to
import ctypes, because it couldn't have any effect on the problem at hand.
Since then I see that you've succeeded with the __int__() method call,
and your problem is in convincing the system to call __long__() when
needed, instead of __int__(). The problem is that the formatting string
%X doesn't distinguish between int and long (and starting with Python 3,
there is no distinction), so apparently it's always calling __int__(),
and ignoring the __long__ unless you explicitly cast it.
If I were you I'd add a cast to the __int__() method, and let the user
use a long() cast if he wants more than 32 bits precision. Or just add
another method tohex() or some such, and have it return a string. Such
a method could take an explicit length argument, which could default to
the actual size of the int or long.
def __int(self):
return int(self.val)
mark.seagoe at gmail.com wrote:
> On Mar 30, 12:01 pm, mark.sea... at gmail.com wrote:
>
>> On Mar 30, 11:53 am, Dave Angel <da... at ieee.org> wrote:
>>
>>
>>> 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.)
>>>
>> Python version: 2.5.1 (r251:54863, Apr 18 2007, 08:51:08)
>> (Thought it was 2.5.3, sorry).
>>
>
> This part is probably important
> Python version: 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310
> 32 bit (Intel)]
>
> Not sure what you mean that you removed the class myclass. Not sure
> how it could work.
>
> Thanks
>
>
More information about the Python-list
mailing list