Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #111570

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

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 <mailman.63.1468755660.2307.python-list@python.org> (permalink)
References <4f796d95-0e87-4610-a0a8-1eff07c60ace@googlegroups.com> <nmfqrg$ea7$1@ger.gmane.org>
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 <python-python-list@m.gmane.org>
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <nmfqrg$ea7$1@ger.gmane.org>
X-Mailman-Original-References <4f796d95-0e87-4610-a0a8-1eff07c60ace@googlegroups.com>
Xref csiph.com comp.lang.python:111570

Show key headers only | View raw


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 "<stdin>", line 1, in <module>
>  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 "<stdin>", line 1, in <module>
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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

can't add variables to instances of built-in classes Kent Tong <kent.tong.mo@gmail.com> - 2016-07-17 03:57 -0700
  Re: can't add variables to instances of built-in classes Peter Otten <__peter__@web.de> - 2016-07-17 13:40 +0200
    Re: can't add variables to instances of built-in classes Wilson Ong <wilsonokw@gmail.com> - 2016-07-17 04:50 -0700
      Re: can't add variables to instances of built-in classes Chris Angelico <rosuav@gmail.com> - 2016-07-17 22:02 +1000
      Re: can't add variables to instances of built-in classes Steven D'Aprano <steve@pearwood.info> - 2016-07-18 01:04 +1000
        Re: can't add variables to instances of built-in classes Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-17 20:00 -0700
          Re: can't add variables to instances of built-in classes Peter Otten <__peter__@web.de> - 2016-07-18 09:38 +0200
            Re: can't add variables to instances of built-in classes Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-18 14:48 -0700
              Re: can't add variables to instances of built-in classes breamoreboy@gmail.com - 2016-07-18 16:12 -0700
                Re: can't add variables to instances of built-in classes Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-18 19:53 -0700
                Re: can't add variables to instances of built-in classes breamoreboy@gmail.com - 2016-07-19 14:24 -0700
                Re: can't add variables to instances of built-in classes Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-19 16:58 -0700
                Re: can't add variables to instances of built-in classes Chris Angelico <rosuav@gmail.com> - 2016-07-20 16:19 +1000
                Re: can't add variables to instances of built-in classes Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-19 23:45 -0700
                Re: can't add variables to instances of built-in classes Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-07-20 17:22 +1000
                Re: can't add variables to instances of built-in classes Peter Otten <__peter__@web.de> - 2016-07-20 09:26 +0200
                Re: can't add variables to instances of built-in classes Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-20 01:50 -0700
                Re: can't add variables to instances of built-in classes Peter Otten <__peter__@web.de> - 2016-07-20 11:15 +0200
                Re: can't add variables to instances of built-in classes Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-20 15:11 -0700
                Re: can't add variables to instances of built-in classes Chris Angelico <rosuav@gmail.com> - 2016-07-21 12:29 +1000
                Re: can't add variables to instances of built-in classes Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-08-03 19:34 -0700
                Re: can't add variables to instances of built-in classes Chris Angelico <rosuav@gmail.com> - 2016-08-04 13:00 +1000
                Re: can't add variables to instances of built-in classes Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-08-04 16:57 -0700
                Re: can't add variables to instances of built-in classes Steven D'Aprano <steve@pearwood.info> - 2016-07-21 12:48 +1000
                Re: can't add variables to instances of built-in classes Steven D'Aprano <steve@pearwood.info> - 2016-07-20 22:10 +1000
                Re: can't add variables to instances of built-in classes Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-20 14:48 -0700
                Re: can't add variables to instances of built-in classes breamoreboy@gmail.com - 2016-07-20 18:04 -0700
                Re: can't add variables to instances of built-in classes Peter Otten <__peter__@web.de> - 2016-07-21 08:59 +0200
              Re: can't add variables to instances of built-in classes Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-20 02:10 -0700
      Re: can't add variables to instances of built-in classes Ethan Furman <ethan@stoneleaf.us> - 2016-07-18 06:10 -0700
  Re: can't add variables to instances of built-in classes Kent Tong <kent.tong.mo@gmail.com> - 2016-07-17 04:53 -0700

csiph-web