Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #70532
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <cameron@cskk.homeip.net> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.020 |
| X-Spam-Evidence | '*H*': 0.96; '*S*': 0.00; 'attribute': 0.07; 'attributes': 0.09; 'pavel': 0.09; 'python': 0.11; 'mostly': 0.14; "'__doc__',": 0.16; '>the': 0.16; 'attributes,': 0.16; 'attributes.': 0.16; 'attributes;': 0.16; 'class:': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'object()': 0.16; 'received:cskk.homeip.net': 0.16; 'received:homeip.net': 0.16; 'simpson': 0.16; 'skip:> 20': 0.16; 'subject:object': 0.16; 'x()': 0.16; 'wrote:': 0.18; 'slightly': 0.19; 'memory': 0.22; 'header:User-Agent:1': 0.23; 'lets': 0.24; 'cheers,': 0.24; 'class.': 0.26; 'pass': 0.26; 'header:In-Reply- To:1': 0.27; 'fixed': 0.29; "skip:' 10": 0.31; '"",': 0.31; 'file': 0.32; 'class': 0.32; 'url:python': 0.33; '(most': 0.33; 'used,': 0.33; "can't": 0.35; 'objects': 0.35; 'but': 0.35; 'add': 0.35; 'object,': 0.36; 'shorter': 0.36; 'skip:> 10': 0.36; 'var': 0.36; 'charset:us-ascii': 0.36; 'url:org': 0.36; 'list': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'explain': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'access,': 0.60; 'tell': 0.60; 'new': 0.61; 'url:3': 0.61; 'simple': 0.61; 'content-disposition:inline': 0.62; "you'll": 0.62; 'different': 0.65; 'hours': 0.66; 'price': 0.69; "'foo'": 0.84; "'object'": 0.84; '(dave': 0.84; 'bored': 0.84; 'footprint': 0.84; 'received:192.168.15': 0.84; 'involved.': 0.91 |
| Date | Wed, 23 Apr 2014 16:11:23 +1000 |
| From | Cameron Simpson <cs@zip.com.au> |
| To | python-list@python.org |
| Subject | Re: object().__dict__ |
| MIME-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii; format=flowed |
| Content-Disposition | inline |
| In-Reply-To | <51d9b7f1-3511-4110-adb2-aa2226bd7a3c@lists.xtsubasa.org> |
| User-Agent | Mutt/1.5.21 (2010-09-15) |
| References | <51d9b7f1-3511-4110-adb2-aa2226bd7a3c@lists.xtsubasa.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.9454.1398233498.18130.python-list@python.org> (permalink) |
| Lines | 52 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1398233498 news.xs4all.nl 2922 [2001:888:2000:d::a6]:40075 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:70532 |
Show key headers only | View raw
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: > >>>>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. 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. 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. 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)
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: object().__dict__ Cameron Simpson <cs@zip.com.au> - 2014-04-23 16:11 +1000
csiph-web