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


Groups > comp.lang.python > #70542

Re: object().__dict__

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <amirouche.boubekki@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'syntax': 0.04; 'value,': 0.04; 'true,': 0.05; 'attribute': 0.07; 'class,': 0.07; 'dynamically': 0.07; 'float': 0.07; 'pypy': 0.07; 'attributes': 0.09; 'builtin': 0.09; 'defines': 0.09; 'pavel': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; 'anyway': 0.14; 'mostly': 0.14; "'__doc__',": 0.16; '__slots__': 0.16; 'attributes,': 0.16; 'attributes.': 0.16; 'attributes;': 0.16; 'class:': 0.16; 'etc...': 0.16; 'foo(object):': 0.16; 'object()': 0.16; 'pypy.': 0.16; 'sentinel': 0.16; 'simpson': 0.16; 'subject:object': 0.16; 'x()': 0.16; 'wrote:': 0.18; 'slightly': 0.19; '>>>': 0.22; 'memory': 0.22; 'cc:addr:python.org': 0.22; 'lets': 0.24; 'cheers,': 0.24; 'cc:2**0': 0.24; 'class.': 0.26; 'define': 0.26; 'equivalent': 0.26; 'pass': 0.26; 'defined': 0.27; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'fixed': 0.29; 'message- id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'code': 0.31; "skip:' 10": 0.31; '"",': 0.31; '>>>>': 0.31; 'int,': 0.31; 'up:': 0.31; 'file': 0.32; 'class': 0.32; 'url:python': 0.33; '(most': 0.33; 'used,': 0.33; "can't": 0.35; 'classes': 0.35; 'except': 0.35; 'something': 0.35; 'objects': 0.35; 'usual': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'object,': 0.36; 'shorter': 0.36; 'var': 0.36; 'url:listinfo': 0.36; 'method': 0.36; 'useful': 0.36; 'url:org': 0.36; 'list': 0.37; 'being': 0.38; 'implement': 0.38; 'skip:& 10': 0.38; 'url:library': 0.38; 'skip:[ 10': 0.38; 'fact': 0.38; 'recent': 0.39; 'explain': 0.39; 'skip:& 20': 0.39; 'does': 0.39; 'url:mail': 0.40; 'how': 0.40; 'access,': 0.60; 'tell': 0.60; 'new': 0.61; 'url:3': 0.61; 'simple': 0.61; 'back': 0.62; 'save': 0.62; "you'll": 0.62; 'kind': 0.63; 'sum': 0.64; 'different': 0.65; 'url:blogspot': 0.65; 'hours': 0.66; 'url:11': 0.68; 'price': 0.69; "'foo'": 0.84; "'object'": 0.84; '(dave': 0.84; 'bored': 0.84; 'footprint': 0.84; 'harmful': 0.84; 'object:': 0.84; 'or...': 0.84; 'involved.': 0.91; 'url:fr': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=oIPMfTA3ucjoMkdOCeR8QLfcgb2aMCEMmAGvdoVZyYY=; b=fZxpnwRB9Fk3d3PGcriJtw7inaFKF/E04fBjaIFtf2RX7wyv9Y8BptPzcDHg/l9BIE R/7CUkpL1ArXsVVSudj/xcw1DKy8WsTjgKeReokn7ShxliAGpCf8IOEBm1Qwjq9l0Nut S9rrnCLITB4ilPbhu3fAk4jLOrdQh/HYwnaJXLj39cHwqza5DFpoizh3c37SL/5YkXr/ /dLIn9dM173S2m9YlRJOB7nYNTiNhtZFP8Be3gqz2WoaNgxHo9nwSakhQZEt546PV2FA Eo69gT3QDNBJ/SO/scNZXSouYxu4zbKGoW+5E/b5WGq9ym1Ut+68JTvqXgtVNr1Nw35F iMSw==
X-Received by 10.236.76.105 with SMTP id a69mr70119795yhe.8.1398260932715; Wed, 23 Apr 2014 06:48:52 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <20140423061123.GA47008@cskk.homeip.net>
References <51d9b7f1-3511-4110-adb2-aa2226bd7a3c@lists.xtsubasa.org> <20140423061123.GA47008@cskk.homeip.net>
From Amirouche Boubekki <amirouche.boubekki@gmail.com>
Date Wed, 23 Apr 2014 15:48:32 +0200
Subject Re: object().__dict__
To Cameron Simpson <cs@zip.com.au>
Content-Type multipart/alternative; boundary=20cf303ea71232acd904f7b5fee7
Cc python-list <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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>
Newsgroups comp.lang.python
Message-ID <mailman.9465.1398260942.18130.python-list@python.org> (permalink)
Lines 297
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1398260942 news.xs4all.nl 2908 [2001:888:2000:d::a6]:55811
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:70542

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

2014-04-23 8:11 GMT+02:00 Cameron Simpson <cs@zip.com.au>:

> On 23Apr2014 09:39, Pavel Volkov <sailor@lists.xtsubasa.org> wrote:
>
>> There are some basics about Python objects I don't understand.
>> Consider this snippet:
>>
>>  class X: pass
>>>>>
>>>> ...
>>
>>>  x = X()
>>>>> dir(x)
>>>>>
>>>> ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__',
>> '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__',
>> '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__',
>> '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
>> '__str__', '__subclasshook__', '__weakref__']
>>
>>>  x.foo = 11
>>>>>
>>>>
>> And now I want to make a simple object in a shorter way, without
>> declaring X class:
>>
>
If you don't want to go through the usual syntax that defines classes you
can use the equivalent code using type to dynamically create a class:

MyClassObject = type('MyClassObject', (object,), dict())

Mind the fact that MyClassObject is a specific kind of object: a class, a
priori, not harmful in anyway


>
>>  y = object()
>>>>> dir(y)
>>>>>
>>>> ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__',
>> '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__',
>> '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__',
>> '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',
>> '__subclasshook__']
>>
>>>  y.foo = 12
>>>>>
>>>> Traceback (most recent call last):
>> File "<stdin>", line 1, in <module>
>> AttributeError: 'object' object has no attribute 'foo'
>>
>> The attribute list is different now and there's no __dict__ and the
>> object does not accept new attributes.
>> Please explain what's going on.
>>
>
> The base "object" class has a fixed set of attributes; you can't add more.
>

Just like any other builtin type: int, float etc... which also means you
can't add method to them dynamically.

Mind the fact that an instance of object is still useful to implement
efficient SENTINEL objects


>
> Almost every other class lets you add attributes, but the price for that
> is that it is slightly in memory footprint and slower to access.
>

class defined in Python except if you define a __slots__


>
> Look up the "__slots__" dunder var in the Python doco index:
>
>   https://docs.python.org/3/glossary.html#term-slots
>
> You'll see it as a (rarely used, mostly discouraged) way to force a fixed
> set of attributes onto a class. As with object, this brings a smaller
> memory footprint and faster attribute access, but the price is flexibility.
>

True, still can be the only way to save few MB or... GB without falling
back to C or PyPy.

Have a look at PyPy to how to save memory (and speed things up) without
slots:
http://morepypy.blogspot.fr/2010/11/efficiently-implementing-python-objects.html

In Python 3 there is a class that is equivalent to:

class foo(object):
    pass

simple object with a __dict__, I can't find it anymore and also there is
SimpleNamespace<https://docs.python.org/3/library/types.html#types.SimpleNamespace>class.

To sum up:

- If you want to create a sentinel value, use object()
- If you want to create an object to hold values and access them as
attributes, use something like SimpleNamespace


> Cheers,
> Cameron Simpson <cs@zip.com.au>
>
> Try being nothing but bored for 4 hours straight, and then tell me that
> there's no fear involved.       - dave@elxr.jpl.nasa.gov (Dave Hayes)
> --
> https://mail.python.org/mailman/listinfo/python-list
>

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


Thread

Re: object().__dict__ Amirouche Boubekki <amirouche.boubekki@gmail.com> - 2014-04-23 15:48 +0200

csiph-web