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

Scott David Daniels Scott.Daniels at Acm.Org
Mon Mar 30 18:40:05 CEST 2009


mark.seagoe at gmail.com wrote:
> ...
> 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.

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
Scott.Daniels at Acm.Org



More information about the Python-list mailing list