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


Groups > comp.lang.python > #103731

Re: Dynamic object attribute creation

From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: Dynamic object attribute creation
Date 2016-02-29 09:33 -0700
Message-ID <mailman.23.1456763676.20602.python-list@python.org> (permalink)
References <56d46597$0$22756$426a74cc@news.free.fr> <1456761964.822300.535132330.33A18241@webmail.messagingengine.com>

Show all headers | View raw


On Mon, Feb 29, 2016 at 9:06 AM, Random832 <random832@fastmail.com> wrote:
> On Mon, Feb 29, 2016, at 10:36, ast wrote:
>> but why doesn't it work with built-in classes int, float, list.... ?
>>
>> L = [1, 8, 0]
>> L.test = 'its a list !'
>>
>> (however lists are mutable, int, float ... are not)
>
> Because those classes do not have attribute dictionaries, in order to
> save space.
>
> You can make a class without an attribute dictionary, by using
> __slots__.

You can also make a list that does have an attribute dictionary by
subclassing it.

>>> class MyList(list): pass
...
>>> obj = MyList()
>>> obj.test = 'foo'

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


Thread

Dynamic object attribute creation "ast" <nomail@invalid.com> - 2016-02-29 16:36 +0100
  Re: Dynamic object attribute creation Random832 <random832@fastmail.com> - 2016-02-29 11:06 -0500
  Re: Dynamic object attribute creation Ian Kelly <ian.g.kelly@gmail.com> - 2016-02-29 09:33 -0700

csiph-web