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

M

mark.seagoe

Hi. So now I have this class that works to allow me to pass in my
reg_info struct. However when I try to make it part of my class it
gets an error "global name 'self' is not defined. I've never seen
this error before. If I comment out the line below 'self.reg_info =
reg_info" then the code runs... but I need to add stuff to this class,
and so I'm wondering why I can't use 'self' anymore?

from ctypes import Structure, c_byte, c_char

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

class myclass(long):
def __new__(class_, init_val, reg_info):
print reg_info.message
self.reg_info = reg_info # <== Offending line
return long.__new__(class_, init_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, my_reg)
print 'cat val = 0x%016X' % cat
print 'type(cat) = %s' % type(cat)

Thanks!
Mark
 
C

Chris Rebert

Hi.  So now I have this class that works to allow me to pass in my
reg_info struct.  However when I try to make it part of my class it
gets an error "global name 'self' is not defined.  I've never seen
this error before.  If I comment out the line below 'self.reg_info =
reg_info" then the code runs... but I need to add stuff to this class,
and so I'm wondering why I can't use 'self' anymore?

`self` is not magical or a language keyword. It's just the
conventional name for the first argument to an instance method (which
is the instance the method is acting upon). For example, a small
minority use `s` instead for brevity. There is no variable `self` in
the parameters of __new__(), hence you get a NameError, as you would
when trying to access any other nonexistent variable.

Also, you shouldn't use `class_ ` as the name of the first argument to
__new__(). Use `cls` instead since that's the conventional name for
it.

My best guess as to what you're trying to do is (completely untested):
class myclass(long):
def __new__(cls, init_val, reg_info):
print reg_info.message
instance = long.__new__(cls, init_val)
instance.reg_info = reg_info
return instance

Cheers,
Chris
 
M

mark.seagoe

`self` is not magical or a language keyword. It's just the
conventional name for the first argument to an instance method (which
is the instance the method is acting upon). For example, a small
minority use `s` instead for brevity. There is no variable `self` in
the parameters of __new__(), hence you get a NameError, as you would
when trying to access any other nonexistent variable.

Also, you shouldn't use `class_ ` as the name of the first argument to
__new__(). Use `cls` instead since that's the conventional name for
it.

My best guess as to what you're trying to do is (completely untested):
class myclass(long):
    def __new__(cls, init_val, reg_info):
        print reg_info.message
        instance = long.__new__(cls, init_val)
        instance.reg_info = reg_info
        return instance

Cheers,
Chris

Thanks Chris. It was tested working (with the offending line editted
out).
Yep this is helpful - I thought 'self' was a keyword. And I see I
should differentiate the instance methods and objects from the cls
reference.
 
C

Chris Rebert

2009/3/29 Scott David Daniels said:
Actually, according to PEP 8, class_ is the preferred name.

In other contexts where you have a class as a variable, yes, but not
in the case of classmethods such as this. See the docs for __new__
itself for example
(http://docs.python.org/reference/datamodel.html#object.__new__).
Normally, these changes are done in the __init__ phase (post-instance
creation), so you might go for something like:

I think the whole reason he's using __new__ instead of __init__ is
because of this tidbit from the aforementioned __new__() docs:
"""
__new__() is intended mainly to allow subclasses of immutable types
(like int, str, or tuple) to customize instance creation. It is also
commonly overridden in custom metaclasses in order to customize class
creation.
"""

Cheers,
Chris
 
M

mark.seagoe

In other contexts where you have a class as a variable, yes, but not
in the case of classmethods such as this. See the docs for __new__
itself for example
(http://docs.python.org/reference/datamodel.html#object.__new__).



I think the whole reason he's using __new__ instead of __init__ is
because of this tidbit from the aforementioned __new__() docs:
"""
__new__() is intended mainly to allow subclasses of immutable types
(like int, str, or tuple) to customize instance creation. It is also
commonly overridden in custom metaclasses in order to customize class
creation.
"""

Cheers,
Chris

It seems like there's no way to do what I'm trying. I am confined to
Python 2.5.3 for business reasons.

So I want a class ShadowRegister, which just has a value that I can do
get/set bit sel and slice ops. I got that working with __init__. It
was subclass from "object". Then I wanted a RegisterClass that was a
subclass of ShadowRegister, which would read a hardware register
before doing get bit sel/slices, or read HW reg, do set bit sel/slice,
but when I try to print in hex format ('0x016X') it said it required
an int (but the ShadowRegister class had no issues). Then I was told
instead of using object I could subclass as long (seemed the only
solution for Python 2.5). Then when I started to want to add my own
init code (like register length in bits), I was told I should use
__new__ instead of __init__. So but ever since then I've noticed that
my value is not changing from the initially set value. I'm really
cornfused now.
 
H

Hrvoje Niksic

So I want a class ShadowRegister, which just has a value that I can
do get/set bit sel and slice ops. I got that working with __init__.
It was subclass from "object". Then I wanted a RegisterClass that
was a subclass of ShadowRegister, which would read a hardware
register before doing get bit sel/slices, or read HW reg, do set bit
sel/slice, but when I try to print in hex format ('0x016X') it said
it required an int (but the ShadowRegister class had no issues).
Then I was told instead of using object I could subclass as long
(seemed the only solution for Python 2.5). Then when I started to
want to add my own init code (like register length in bits), I was
told I should use __new__ instead of __init__. So but ever since
then I've noticed that my value is not changing from the initially
set value. I'm really cornfused now.

I think I understand your problem. The short story is: if you derive
from int or long, you won't be able to change the "value" because the
underlying value is immutable. To get mutable values, you'll need to
subclass object and implement the int-like functionality you need.
Fortunately, this is quite easy.

I assume that by "print in hex format" you're referring to the %
operator, such as '%x' % your_instance. For it to work, you need to
define an __int__ method (not to be confused with __init__!), which
will get called when coercion to integer is required. For example:

class Foo(object):
def __init__(self, initval=0):
self._value = initval
# a bunch of methods for getting/setting bits
def __int__(self):
return self._value
'0x3e8'
 
J

Jan Decaluwe

Python 2.5.3 for business reasons.

So I want a class ShadowRegister, which just has a value that I can do
get/set bit sel and slice ops. I got that working with __init__. It
was subclass from "object". Then I wanted a RegisterClass that was a
subclass of ShadowRegister, which would read a hardware register
before doing get bit sel/slices, or read HW reg, do set bit sel/slice,
but when I try to print in hex format ('0x016X') it said it required
an int (but the ShadowRegister class had no issues). Then I was told
instead of using object I could subclass as long (seemed the only
solution for Python 2.5). Then when I started to want to add my own
init code (like register length in bits), I was told I should use
__new__ instead of __init__. So but ever since then I've noticed that
my value is not changing from the initially set value. I'm really
cornfused now.

In the past, someone referred you to the intbv class in MyHDL.
You mentioned that it does "more than you want".
However, it seems to me that what intbv really does, is to solve
the kind of issues that you are struggling with. Perhaps
you want to look at it again.

Jan

--
Jan Decaluwe - Resources bvba - http://www.jandecaluwe.com
Python as an HDL: http://www.myhdl.org
VHDL development, the modern way: http://www.sigasi.com
Analog design automation: http://www.mephisto-da.com
World-class digital design: http://www.easics.com
 
M

mark.seagoe

In the past, someone referred you to the intbv class in MyHDL.
You mentioned that it does "more than you want".
However, it seems to me that what intbv really does, is to solve
the kind of issues that you are struggling with. Perhaps
you want to look at it again.

Jan

--
Jan Decaluwe - Resources bvba -http://www.jandecaluwe.com
    Python as an HDL:http://www.myhdl.org
    VHDL development, the modern way:http://www.sigasi.com
    Analog design automation:http://www.mephisto-da.com
    World-class digital design:http://www.easics.com- Hide quoted text -

- Show quoted text -

Hi Hrvoje, Jan;

This is about what I actually originally had, it was derived from
"object", like the intbv class from MyHDL. And so my ShadowRegister
class was modeled similar to that. It worked grea. Then ChipRegister
was made to be a subclass of ShadowRegister. It needs to read HW reg
and adjust ChipRegister value, write HW reg and adjust ShadowRegister
value. As mentioned, that's when printing in hex gave an error said
it needed an int. My story from that point continues from my last
past. :)
 
M

mark.seagoe

It needs to read HW reg and adjust ChipRegister value, or
ShadowRegister and change the HW value (order was reversed). I will
look at MyHDL again though to see if there is some reason it can print
class subclassed from intbv, or if it even has a class subclassed from
intbv, without casting as a long in the main program.
 
M

mark.seagoe

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
 
M

MRAB

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

Don't you really mean:

self.val = init_val
self.size = reg_info.size
print 'address = 0x%02X' % self.reg_info.address
# return self
[snip]
 
D

David Bolen

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:
Traceback (most recent call last):

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

-- David
 
M

mark.seagoe

OK, you have asked questions at narrow points about how you can get
whatever is your local problem at the moment, and have received answers
addressing your question.  In effect, you've asked 'Which way to the
apothecary?' and 'How do I get around this building' without mentioning
that you're looking for how to get to the top of Everest.  Perhaps you
even mentioned your whole goal on your first post, but people see the
questions they see.

You cannot subclass immutable values to get mutable values; a number
of the properties of immutables are what allows them to participate
in such things as sets and dictionaries.  Such things aren't reasonable
for mutable Registers, unless the identity of the register is the thing
that distinguishes it.  A set of three Registers, all of which currently
have the value 5 stored in them had better be a 3-element set, or when
you change one of them without changing the other two the size of your
set will have to magically change.

On a recent job, I was dealing with registers in a device and bit
accesses for reads and writes.  I made a pair of classes:  One to
correspond to the device itself (with only one instance per chunk of
hardware attached), and another class representing possible values of
the register at given moments.  The RegisterValue class was a subclass
of int, and had a from_name class method, mask (&) and set (|)
operations and a repr that show the non-zero bits connected by '|'.
The Register class had read, read_masked, write, and write_masked
operations.  It worked quite well.  Actually, there were more than
a hundred RegisterValue classes, most of which were subclasses of the
base RegisterValue class with different bit name lists.

Remember that just because a Register might have a name, doesn't mean
its string representations don't necessarily reflect the contents of
the register (lists show their contents, for example).  I think you'll
do better to separate the idea of a register and its contents.

--Scott David Daniels
(e-mail address removed)- Hide quoted text -

- Show quoted text -

Thanks Scott. I think you are saying don't try to subclass from type
long, because it is immutable (cannot be changed). If I subclass from
type object, then the value can be changed, but I still can't print
without explicitely casting back to long in the main routine. Is this
a catch 22 scenario, or am I just overlooking some method of long to
override to change it's return value?

Here is what I currently have morphed to:

""" Works to change self.val but not self"""

from ctypes import *

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

class BigNumber(long):
def __new__(class_, init_val, size):
print 'printing size: %d' % size
self = long.__new__(class_, init_val)
self.size = size
self.val = self
return self
#
def __repr__(self):
print 'from __repr__: %s' % self
return '%s(%s)' % (type(self).__name__, 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)

class HugeNumber(BigNumber):
def __new__(class_, reg):
return BigNumber.__new__(class_, reg.init, reg.size)


#
my_reg = REG_INFO()
my_reg.address = 0xab
my_reg.message = 'hello world'
my_reg.size = 32

print '\nTEST 1'
my_reg.init = 0x55
s = HugeNumber(my_reg)
print 'dog = %r = %016X' % (s, s)

print '\nTEST 2'
my_reg.init = 0x123456789ABCDEF0
bird = HugeNumber(my_reg)
print 'type(bird) = %s' % type(bird)
print 'bird = 0x%016X' % bird

print '\nTEST 3'
print 'bird.val = 0x%016X' % bird.val
print 'bird = 0x%016X' % bird
print 'bird[0] val = 0x%01X' % bird[0]
bird[0] = ~bird[0]
print 'bird.val = 0x%016X' % bird.val
print 'bird = 0x%016X' % bird
print 'bird[0] val = 0x%01X' % bird[0]

Run it:

TEST 1
printing size: 32
from __repr__: 85
dog = HugeNumber(85) = 0000000000000055

TEST 2
printing size: 32
type(bird) = <class '__main__.HugeNumber'>
bird = 0x000000009ABCDEF0

TEST 3
bird.val = 0x000000009ABCDEF0
bird = 0x000000009ABCDEF0
bird[0] val = 0x0
bird.val = 0x000000009ABCDEF1
bird = 0x000000009ABCDEF0 <== Value did not change (because immutable
I guess)
bird[0] val = 0x1
 
M

mark.seagoe

Note that here you assign self.val to be the object itself.  Are you
sure you didn't mean "self.val = init_val"?


Instead of an integer, you return the current class instance as set up
in __init__.  The __int__ method ought to return an integer.


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
 
M

mark.seagoe

If I cast it long then it prints fine, but I was hoping there would be
a slicker way (to accomplish this in the class itself).

print 'bird val = 0x%016X' % long(bird)
 
D

Dave Angel

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.)

Note that here you assign self.val to be the object itself. Are you
sure you didn't mean "self.val =nit_val"?


Instead of an integer, you return the current class instance as set up
in __init__. The __int__ method ought to return an integer.


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:

Traceback (most recent call last):


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
 
M

mark.seagoe

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).
 
M

mark.seagoe

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
 
M

mark.seagoe

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- Hide quoted text -

- Show quoted text -

I can see now that this is futile. Upgrading to 2.6. I know when
I've been whooped. Thanks.

Mark
 
D

Dave Angel

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)



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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top