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: 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 Date: Wed, 23 Apr 2014 15:48:32 +0200 Subject: Re: object().__dict__ To: Cameron Simpson Content-Type: multipart/alternative; boundary=20cf303ea71232acd904f7b5fee7 Cc: python-list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 --20cf303ea71232acd904f7b5fee7 Content-Type: text/plain; charset=UTF-8 2014-04-23 8:11 GMT+02:00 Cameron Simpson : > On 23Apr2014 09:39, Pavel Volkov 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 "", line 1, in >> 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 SimpleNamespaceclass. 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 > > 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 > --20cf303ea71232acd904f7b5fee7 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable



2014-04-23 8:11 GMT+02:00 Cameron Simpson <cs@zip.com.au>:
On 23Apr2014 09:39, Pavel Volkov <sailor@lists.xts= ubasa.org> wrote:
There are some basics about Python objects I don't understand.
Consider this snippet:

class X: pass
...
x =3D X()
dir(x)
['__class__', '__delattr__', '__dict__', '__dir= __', '__doc__', '__eq__', '__format__', '__= ge__', '__getattribute__', '__gt__', '__hash__'= , '__init__', '__le__', '__lt__', '__module__&#= 39;, '__ne__', '__new__', '__reduce__', '__redu= ce_ex__', '__repr__', '__setattr__', '__sizeof__= 9;, '__str__', '__subclasshook__', '__weakref__']
x.foo =3D 11

And now I want to make a simple object in a shorter way, without declaring = X class:

If yo= u don't want to go through the usual syntax that defines classes you ca= n use the equivalent code using type to dynamically create a class:

MyClassObject =3D type('MyClassObject', (object,), d= ict())

Mind the fact that MyClassObject is a specific kin= d of object: a class, a priori, not harmful in anyway
=C2=A0=

y =3D object()
dir(y)
['__class__', '__delattr__', '__dir__', '__doc_= _', '__eq__', '__format__', '__ge__', '__ge= tattribute__', '__gt__', '__hash__', '__init__'= , '__le__', '__lt__', '__ne__', '__new__', = '__reduce__', '__reduce_ex__', '__repr__', '__s= etattr__', '__sizeof__', '__str__', '__subclasshook= __']
y.foo =3D 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 obj= ect does not accept new attributes.
Please explain what's going on.

The base "object" class has a fixed set of attributes; you can= 9;t add more.

Just like any other built= in type: int, float etc... which also means you can't add method to the= m dynamically.

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

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 __sl= ots__
=C2=A0

Look up the "__slots__" dunder var in the Python doco index:

=C2=A0 https://docs.python.org/3/glossary.html#term-slots<= br>
You'll see it as a (rarely used, mostly discouraged) way to force a fix= ed set of attributes onto a class. As with object, this brings a smaller me= mory footprint and faster attribute access, but the price is flexibility.

True, still can be the only way to save fe= w 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-im= plementing-python-objects.html

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

c= lass foo(object):
=C2=A0=C2=A0=C2=A0 pass

s= imple object with a __dict__, I can't find it anymore and also there is= SimpleNamespace class.
=C2=A0
To sum up:

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


Cheers,
Cameron Simpson <cs@z= ip.com.au>

Try being nothing but bored for 4 hours straight, and then tell me that
there's no fear involved. =C2=A0 =C2=A0 =C2=A0 - dave@elxr.jpl.nasa.gov (Dave Haye= s)
--
https://mail.python.org/mailman/listinfo/python-list

--20cf303ea71232acd904f7b5fee7--