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

Peter Otten __peter__ at web.de
Wed Jul 20 03:26:08 EDT 2016


Lawrence D’Oliveiro wrote:

> On Wednesday, July 20, 2016 at 6:19:45 PM UTC+12, Chris Angelico wrote:
>>
>> On Wed, Jul 20, 2016 at 9:58 AM, Lawrence D’Oliveiro wrote:
>>>
>>> On Wednesday, July 20, 2016 at 9:24:57 AM UTC+12, bream... at gmail.com
>>> wrote:
>>>>
>>>> On Tuesday, July 19, 2016 at 3:54:12 AM UTC+1, Lawrence D’Oliveiro
>>>> wrote:
>>>>>
>>>>> On Tuesday, July 19, 2016 at 11:12:52 AM UTC+12, bream... at gmail.com
>>>>> wrote:
>>>>>>
>>>>>> On Monday, July 18, 2016 at 10:48:15 PM UTC+1, Lawrence D’Oliveiro
>>>>>> wrote:
>>>>>>>
>>>>>>> <https://github.com/ldo/qahirah>
>>>>>>> When you have lots of read/write properties, I find __slots__ to be
>>>>>>> a good idea.
>>>>>>
>>>>>> Please explain why, thank you.
>>>>>
>>>>> I was trying something like
>>>>>
>>>>>     ctx.dashes = ((0.1, 0.03, 0.03, 0.03), 0)
>>>>>
>>>>> and wondering why it wasn’t working...
>>>>
>>>> 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”.
> 
>> This sounds like the sort of cargo cult debugging that I'd expect of PHP
>> programmers ("I put addslashes around everything and now it works, so in
>> future I'll use addslashes everywhere"), but around here, we're better
>> than that.
> 
> OK, boss.

pylint can detect candidates for accidental attribute creation:

$ cat attrib.py
class Context:
    def __init__(self):
        self.dashes = None

ctx = Context()
ctx.dasehs = ("foo", "bar")
$ pylint attrib | grep dasehs
W:  6, 4: Attribute 'dasehs' defined outside __init__ (attribute-defined-
outside-init)





More information about the Python-list mailing list