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

Chris Rebert clp2 at rebertia.com
Mon Mar 30 02:16:54 EDT 2009


2009/3/29 Scott David Daniels <Scott.Daniels at acm.org>:
> mark.seagoe 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



More information about the Python-list mailing list