Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: can't add variables to instances of built-in classes Date: Sun, 17 Jul 2016 13:40:31 +0200 Organization: None Lines: 52 Message-ID: References: <4f796d95-0e87-4610-a0a8-1eff07c60ace@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de r00yK3e9vte5V9hNX0n9IAnpXAv1p4wRb1wKOcYXv3Ig== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'memory.': 0.05; 'dict': 0.09; 'instances.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subclass': 0.09; 'variables': 0.15; '__slots__': 0.16; 'attributes.': 0.16; 'attributes:': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'test()': 0.16; 'user-defined': 0.16; 'wrote:': 0.16; 'string': 0.17; 'attribute': 0.18; 'integer': 0.18; '>>>': 0.20; '"",': 0.22; 'pass': 0.22; '(most': 0.24; 'feature': 0.24; 'header:User- Agent:1': 0.26; "doesn't": 0.26; 'header:X-Complaints-To:1': 0.26; 'dictionary': 0.29; 'str': 0.29; 'classes': 0.30; 'waste': 0.30; 'class': 0.33; 'belong': 0.33; 'instances': 0.33; 'traceback': 0.33; 'file': 0.34; 'add': 0.34; 'list:': 0.35; 'but': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'hi,': 0.38; 'test': 0.39; 'subject:-': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'your': 0.60; 'avoid': 0.61; 'default': 0.61; "'test'": 0.84; 'kent': 0.84; 'subject:add': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd8de5.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 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: X-Mailman-Original-References: <4f796d95-0e87-4610-a0a8-1eff07c60ace@googlegroups.com> Xref: csiph.com comp.lang.python:111570 Kent Tong wrote: > Hi, > > I can add new variables to user-defined classes like: > >>>> class Test: > ... pass > ... >>>> a=Test() >>>> a.x=100 > > but it doesn't work if the instances belong to a built-in class such as > str or list: > >>>> a='abc' >>>> a.x=100 > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'str' object has no attribute 'x' > > What makes this difference? By default custom classes have a dictionary (called __dict__) to hold these attributes. If for every string or integer there were such a dict that would waste a lot of memory. You can subclass if you need it: >>> class Str(str): pass ... >>> s = Str("hello") >>> s.x = 42 >>> s 'hello' >>> s.x 42 You can also avoid the dict in your own classes by specifiying slots for allowed attributes: >>> class Test: ... __slots__ = ("foo", "bar") ... >>> t = Test() >>> t.foo = 42 >>> t.baz = "whatever" Traceback (most recent call last): File "", line 1, in AttributeError: 'Test' object has no attribute 'baz' Use this feature sparingly, only when you know that there are going to be many (millions rather than thousands) of Test instances.