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

mark.seagoe at gmail.com mark.seagoe at gmail.com
Mon Mar 30 09:13:24 CEST 2009


On Mar 29, 11:16 pm, Chris Rebert <c... at rebertia.com> wrote:
> 2009/3/29 Scott David Daniels <Scott.Dani... at acm.org>:
>
> > mark.sea... at gmail.com wrote:
>
> >> On Mar 29, 9:52 pm, Chris Rebert <c... at rebertia.com> wrote:
>
> >>> On Sun, Mar 29, 2009 at 9:18 PM,  <mark.sea... at gmail.com> wrote:
>
> >>>> ...
>
> >>> ... 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.
>
> > 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__).
>
> >>> 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
>
> > 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
>
> --
> I have a blog:http://blog.rebertia.com

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.



More information about the Python-list mailing list