Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ethan Furman Newsgroups: comp.lang.python Subject: Re: can't add variables to instances of built-in classes Date: Mon, 18 Jul 2016 06:10:08 -0700 Lines: 18 Message-ID: References: <4f796d95-0e87-4610-a0a8-1eff07c60ace@googlegroups.com> <578CD530.5090702@stoneleaf.us> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de /wJVkRt6FeMcUsZGgGVAdgtpvrLkxEXMZz06tvY8bunA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'attributes': 0.07; 'objects,': 0.07; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'instances.': 0.09; 'message-id:@stoneleaf.us': 0.09; 'python': 0.10; '__slots__': 0.16; 'instances,': 0.16; 'only?': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'memory': 0.17; 'debugging': 0.18; "shouldn't": 0.18; 'am,': 0.23; 'defined': 0.23; 'header:In-Reply-To:1': 0.24; 'feature': 0.24; 'header:User-Agent:1': 0.26; 'accomplished': 0.29; '~ethan~': 0.29; 'objects': 0.29; "i'm": 0.30; 'class': 0.33; 'but': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'things': 0.38; 'why': 0.39; 'test': 0.39; 'sure': 0.39; 'subject:-': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'skip:u 10': 0.61; 'lost,': 0.84; 'subject:add': 0.91; 'wilson': 0.91 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <578CD530.5090702@stoneleaf.us> X-Mailman-Original-References: <4f796d95-0e87-4610-a0a8-1eff07c60ace@googlegroups.com> Xref: csiph.com comp.lang.python:111605 On 07/17/2016 04:50 AM, Wilson Ong wrote: >> Use this feature sparingly, only when you know that there are going to be >> many (millions rather than thousands) of Test instances. > > Why use it sparingly? Is it for extensibility? What if I'm pretty sure that > my class is going to have exactly these attributes only? One of the very nice things about Python is being able to fiddle with objects, both for debugging and for extending. One of the ways that's accomplished is by adding attributes to instances, but when __slots__ is defined that ability is lost, which can make using/debugging those types of objects harder. __slots__ is a memory optimization, and like most optimazations you shouldn't use it until you know you need it. -- ~Ethan~