can't add variables to instances of built-in classes

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Jul 20 03:22:15 EDT 2016


On Wednesday 20 July 2016 16:45, Lawrence D’Oliveiro wrote:

>>>>> I was trying something like
>>>>>
>>>>>     ctx.dashes = ((0.1, 0.03, 0.03, 0.03), 0)
>>>>>
>>>>> and wondering why it wasn’t working...

And so are we. Since you've already solved the problem, maybe you could 
enlighten us?

>>>> This makes no sense to me at all.  You appear to be trying to create a
>>>> tuple, which contains a tuple and an integer.  You then say it doesn't
>>>> work, but imply that using __slots__ fixes the problem.  So please explain
>>>> exactly what you were trying to achieve, the exact error you got, with the
>>>> complete traceback, and how using __slots__ fixed the problem.
>>>
>>> No traceback. The lines were simply coming out solid, instead of dashed.
>> 
>> And __slots__ fixed the problem how, exactly?
> 
> The Context attribute that controls the dash settings is called “dash”, not
> “dashes”.

Ah, finally, an actually *useful* reply. Honestly Lawrence, couldn't you have 
just said this right at the start, instead of having us drag the answer out of 
you over multiple email exchanges?

If you had said right at the beginning "I have a habit of mistyping attribute 
names, and using __slots__ ensures I get an immediate error" then we would have 
understood you. Instead, you waste our time, *and yours*, leading us up the 
garden path by implying that you fixed a display bug by changing something 
like:

class Context(object):
    def __init__(self):
        self.dashes = something

to:

class Context(object):
    __slots__ = ("dashes",)
    def __init__(self):
        self.dashes = something

Perhaps now you understand why you gave us the impression of cargo-cult 
debugging.




-- 
Steve




More information about the Python-list mailing list